404处理程序获取"句柄未初始化"异常

Mic*_*ael 5 asp.net iis-7

我使用下面的方法来处理我的网站上的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>
Run Code Online (Sandbox Code Playgroud)

在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.");
  }
}
Run Code Online (Sandbox Code Playgroud)

例外细节:

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)
Run Code Online (Sandbox Code Playgroud)

Mic*_*ael 0

Levi:我们已经找到了根本原因,正在等待修复。

Andrew McLachlan确认已在 .NET 4.5 RC 中修复