![](/img/trans.png)
[英]Why I get different date formats when I run my application through IIS and Visual Studio's web server on the same computer?
[英]My Visual Studio 2008 web application keeps throwing a .Net error when I first run it, but refreshing fixes it
如標題所述,我的Web應用程序成功構建,盡管每次以調試模式運行它時,都會出現以下.Net錯誤:
如果我單擊“刷新”,則該應用程序不會再出現錯誤,直到我下次再次啟動它,您有什么想法嗎?
這是我的global.asax文件:
<%@ Application Language="C#" Inherits="MyCompany.Web.MyApp.Shell.CustomWebClientApplication" %>
<script runat="server">
void Session_End(Object Sender, EventArgs e)
{
FormsAuthentication.SignOut();
}
protected void Session_Start(Object sender, EventArgs e)
{
if (Session.IsNewSession)
{
Response.Redirect(System.Web.Configuration.WebConfigurationManager.AppSettings["HomePage"]);
}
}
protected void Application_Error(Object sender, EventArgs e)
{
System.Exception oops = Server.GetLastError();
//Injection attack error handling
if (oops.GetBaseException() is System.Web.HttpRequestValidationException)
{
Response.Redirect("ErrorPage.aspx");
}
}
</script>
您正在嘗試訪問設置為null(或尚未初始化)的變量。 您在Global.asax中是否有任何內容或在應用程序啟動時觸發的內容? 您是否有在應用程序啟動時觸發的異步操作?
檢查您的Home.aspx頁以查看發生了什么。 看來您的應用程序已重定向到該頁面,因此我猜想在init或page_load事件上有一些正在執行的事件會導致問題。
System.Exception oops
我認為這是問題的根源。 沒有對象返回時
Server.GetLastError();
那么你會在網上得到NullReferenceException
oops.GetBaseException()
這是很合理的。 第一次運行時,oops為null(因為之前沒有發生錯誤),因此拋出NullReferenceException。 在第二頁刷新時,GetLastError()返回具有先前錯誤(NullReferenceException)的對象,並顯示頁面。 在訪問對象之前,請始終檢查對象是否為null。
無論如何,您始終可以嘗試捕獲所有運行時異常(Debug-> Exceptions-> Common Language runtime),並查看問題出在哪里。
聽起來像是初始化問題。 您可能正在嘗試在資源初始化之前使用它,但是在刷新時,已經有時間進行初始化了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.