第二页Page_Load上的NullReferenceException

时间:2013-04-09 17:28:42

标签: c# asp.net .net nullreferenceexception

起初我想说:是的,我知道有很多类似于我的问题,但不一样。

当我在我的开发者机器上启动我的12个站点中的一个时,一切都很棒,并且在服务器11上也没有问题。

当我启动第12个站点时,它首先工作正常,但是当它导致回发时(Button,带有AutoPostBack的DropDownList等等)我得到以下错误:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   Infoscreen.Anzeigeeinstellungen.Page_Load(Object sender, EventArgs e) in C:\Users\Krusty\Desktop\Schule\Diplomarbeit\Infoscreen\Infoscreen\Anzeigeeinstellungen.aspx.cs:97
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +24
   System.Web.UI.Control.LoadRecursive() +70
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3047

路径(C:\ Users \ Krusty \ Desktop \ Schule \ Diplomarbeit \ Infoscreen \ Infoscreen \ Anzeigeeinstellungen.aspx.cs)是我的开发人员机器上的文件。 但为什么?? 我从来没有在我的程序中硬编码任何路径,甚至重新创建网站都没有用。

我该怎么办?任何提示/提示将不胜感激。

修改

91    if (!Page.IsPostBack)
92    { 
93        Response.Cookies["Infoscreen_Anzeigeeinstellungen_Ausgewählte_Abteilung"].Value =  ausgewählte_Abteilung.ToString(); 
94    }
95    else
96    { 
97        ausgewählte_Abteilung = Request.Cookies["Infoscreen_Anzeigeeinstellungen_Ausgewählte_Abteilung"].Value; 
98    }

修改

是的,IIS配置为使用Cookie

修改

解决了! 在VisualStudio2010服务器中,char'ä'工作...
在IIS7中,它没有...
因此cookie永远不会被正确设置并且get请求挂起

将cookie命名为“Infoscreen_Anzeigeeinstellungen_Ausgewaehlte_Abteilung”,现在工作正常

可以关闭

1 个答案:

答案 0 :(得分:2)

你已经发现了自己,但仅供将来参考:

在处理cookie的代码中,c#中允许使用'name'(使用a-umlaut)但是根据RFC2616,cookie的标记必须包含US-ASCII字符的子集。

if (!Page.IsPostBack)
    { 
        Response.Cookies["Infoscreen_Anzeigeeinstellungen_Ausgewählte_Abteilung"].Value =  ausgewählte_Abteilung.ToString(); 
    }
    else
    { 
        ausgewählte_Abteilung = Request.Cookies["Infoscreen_Anzeigeeinstellungen_Ausgewählte_Abteilung"].Value; 
    }

因此,如果基于表单/控件名生成cookiekey,则可以使用安全Cookie密钥:

static string TokenRFC2616(string key)
{
    const string separators = "()|<>@,;:\\\"/[]?={} ";
    var chars = from ch in key.Normalize(NormalizationForm.FormD)
            where CharUnicodeInfo.GetUnicodeCategory(ch) 
                     != UnicodeCategory.NonSpacingMark &&
                  separators.IndexOf(ch)==-1
            select ch;
    return String.Concat(chars);
}

string cookiekey = TokenRFC2616(
       "Infoscreen_Anzeigeeinstellungen_Ausgewählte_Abteilung");
if (!Page.IsPostBack)
{ 
   Response.Cookies[cookieKey].Value =  ausgewählte_Abteilung.ToString(); 
}
else
{ 
    ausgewählte_Abteilung = Request.Cookies[cookieKey].Value; 
}

(在上面的示例中,Cookie名称将为Infoscreen_Anzeigeeinstellungen_Ausgewahlte_Abteilung

相关问题