c# - 2nd Page_Load 上的 NullReferenceException

标签 c# asp.net .net nullreferenceexception

首先我想说:是的,我知道有很多问题与我的相似,但又不相同。

当我在我的开发人员机器上启动我的 12 个站点中的一个时,一切都运行良好,而且在服务器上,其中 11 个站点也可以正常运行。

当我启动第 12 个站点时,它首先工作正常,但是当它导致回发时(按钮、带有 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 Server 中,char 'ä' 有效...
在 IIS7 中它不...
所以 cookie 永远不会正确设置并且获取请求挂断

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

可以关闭

最佳答案

因为您已经找到了自己,但仅供将来引用:

在您处理 cookie 的代码中,“名称”在 c# 中是允许的(使用变音符号),但根据 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 是基于表单/控件名称生成的,那么拥有安全 Cookies key 的方法可能是:

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 )

关于c# - 2nd Page_Load 上的 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15908543/

相关文章:

c# - 我真的需要在集合上使用 AsQueryable() 吗?

c# - C# 中的可写字节流风格功能

c# - 如果我想使用 ASP.NET MVC4 创建 ApiKey 受限资源,我应该使用 IAuthorizationFilter 吗?

c# - 如何在没有时间的情况下获取当前日期?

c# - 将 .NET Core 与旧版 .NET 框架 dll 结合使用

c# - 将 USB 设备名称映射到 C# 中的 com 端口?

asp.net - 为什么我的 WCF 服务返回和 ARRAY 而不是 List <T>?

c# - 使用 OnClick 事件与 asp :Button 的 PostBackUrl 属性

javascript - 将 ListView 的行号传递给 JavaScript 函数

c# - 在 C#.NET 中使用 USB PS2 手控器