我使用以下方法处理我网站上的404错误。这已经工作了很长时间,但是在上个月突然间,我在我们的专用服务器上有一些网站(一些仍在工作,并且在开发机器上工作)得到“Handle is not initialized”例外。有人有什么想法吗?
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<httpErrors existingResponse="Replace">
<remove statusCode="500" subStatusCode="-1"/>
<remove statusCode="404" subStatusCode="-1"/>
<error statusCode="404" prefixLanguageFilePath="" path="/default.aspx" responseMode="ExecuteURL"/>
<error statusCode="500" prefixLanguageFilePath="" path="/error.aspx" responseMode="ExecuteURL"/>
</httpErrors>
</system.webServer>
在default.aspx页面上:
protected void Page_PreRender(object sender, EventArgs e)
{
if (!Page.IsPostBack && Request.Url.ToString().Contains("?404;"))
{
HttpContext.Current.RewritePath("~/");
Page.Header.Controls.AddAt(0, new LiteralControl("<base href='" + Request.Url.Scheme + "://" + Request.Url.Authority + "'/>"));
Response.StatusCode = 404;
Util.DisplayAlert("The page you are looking for no longer exists. If you navigated to this page by clicking a link within this site please <a href='/contact.aspx'>contact us</a> to let us know.");
}
}
异常详情:
Exception information:
Exception type: InvalidOperationException
Exception message: Handle is not initialized.
at System.Runtime.InteropServices.GCHandle.FromIntPtr(IntPtr value)
at System.Web.Hosting.PipelineRuntime.GetManagedPrincipalHandler(IntPtr pRootedObjects)
at System.Web.Hosting.UnsafeIISMethods.MgdGetPrincipal(IntPtr pHandler, IntPtr& pToken, IntPtr& ppAuthType, Int32& pcchAuthType, IntPtr& ppUserName, Int32& pcchUserName, IntPtr& pManagedPrincipal)
at System.Web.Hosting.IIS7WorkerRequest.GetUserPrincipal()
at System.Web.Hosting.IIS7WorkerRequest.SynchronizeVariables(HttpContext context)
at System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context)
答案 0 :(得分:2)
我遇到了同样的问题但尚未找到解决方案。我已经确认问题是由响应上的StatusCode或Status属性设置引起的,它会影响IIS 7和IIS Express,但不会影响Cassini,这对于堆栈跟踪是有意义的。
我会继续调查。
编辑:找到解决方案没有好运。我在IIS论坛上发布了同样的问题:http://forums.iis.net/p/1187959/2016914.aspx#2016914
编辑2:在.NET 4.5 RC中确认为已修复。
答案 1 :(得分:0)
Levi:我们已经找到了根本原因并正在排队修复。
Andrew McLachlan在.NET 4.5 RC中确认已修复