【发布时间】:2015-02-11 14:51:36
【问题描述】:
我制作了一个 Web 应用程序,并在本地对其进行了测试。一切正常,因此它被部署到托管服务器。 几周后,一些用户开始遇到“对象引用未设置为对象实例”错误。 目前这只发生在 chrome 浏览器中(在其他浏览器上,应用程序正常启动),如果他们在 chrome 中使用隐身模式,应用程序也可以正常打开。
这是整个错误消息,不幸的是我没有得到错误所在的代码行,即使我使用了
Server Error in '/' Application.
Object reference not set to an instance of an object.
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.]
INAWebAppTest.SiteMaster.Page_Load(Object sender, EventArgs e) +884
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Control.LoadRecursive() +145
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
我不知道这是否是因为服务器上部署了较新版本的应用程序以及用户 cookie?
如果是这样,您能否告知我可以尝试纠正此问题的方法。我不能要求所有用户都清理他们的浏览器历史记录和cookies,它们太多了,如果以后再发生,也无法解决这个问题。
这是来自网站管理员的代码:
public partial class SiteLogin : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["userInfo"] != null)
{
Response.Cookies.Set(Request.Cookies["userInfo"]);
Response.Cookies["userInfo"]["lastVisit"] = DateTime.Now.ToString();
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(7);
if (Server.HtmlEncode(Request.Cookies["userInfo"]["userName"]) == null)
{
Response.Redirect("~/Account/login.aspx");
}
if (Server.HtmlEncode(Request.Cookies["userInfo"]["role"]).Equals("3"))
Response.Redirect("~/Form/Progress.aspx");
if (Server.HtmlEncode(Request.Cookies["userInfo"]["role"]).Equals("2"))
Response.Redirect("~/CForm/CHome.aspx");
if (Server.HtmlEncode(Request.Cookies["userInfo"]["role"]).Equals("1"))
Response.Redirect("~/AForm/AHome.aspx");
}
}
}
}
这是来自 global.asax 页面的代码:
protected void Session_Start(object sender, EventArgs e)
{
string sessionId;
if (Session.SessionID!=null)
sessionId = Session.SessionID;
else
{
SmtpClient client = new SmtpClient();
client.Port = xxx;
client.Host = "xxx.xxx.xxx.xxx";
client.EnableSsl = false;
client.Timeout = 10000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("");
MailMessage mm = new MailMessage();
mm.From = new MailAddress("xxx", "xxx");
mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
mm.Subject = "Null object";
mm.IsBodyHtml = true;
mm.To.Add("xxx");
string body = "null";
mm.Body = body;
try
{
client.Send(mm);
}
catch
{
}
}
在应用的第一个版本中,global.asax 是空的,但是在刷新会话时出现了一些问题,所以我搜索了一下,发现解决方案是这行代码:
string sessionId = Session.SessionID;
这解决了这个问题,但由于一些用户遇到了本主题中提到的问题,我添加了一些代码,以便在找不到会话变量时收到一条消息。我从来没有收到过这封邮件,所以我认为问题不存在。
感谢您的帮助!
【问题讨论】:
-
您是否正在使用和/或设置任何
Session[]变量..?如果你是..那么我会推荐两件事..1.初始化Globala.asax文件中的所有会话变量,2使用HttpContext.Current.Session而不是Session如果这不是问题,请显示引发此错误的相关代码..如果您不确定..然后使用Debugger确定它发生的位置 -
你能把抛出异常的代码贴出来吗,看看它在
Page_Load的SiteMaster中的堆栈跟踪。可能是您在请求中引用的某些内容可能不是从 chrome 设置的 -
感谢您的回答!我编辑了我的问题并添加了来自 global.asax 和 page master 的代码。请您再看一遍好吗?不幸的是我没有这个问题(大多数用户没有报告这个问题),应用程序在 chrome 浏览器中为我正常启动,所以我无法调试它。
标签: c# asp.net google-chrome cookies