加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 404处理程序获取“句柄未初始化”异常

发布时间:2020-12-16 03:21:34 所属栏目:asp.Net 来源:网络整理
导读:我使用下面的方法来处理我的网站上的404错误.这已经工作了很长时间,但是在上个月突然间,我在我们的专用服务器上有一些网站(一些仍在工作,并且在开发机器上工作)得到“Handle is not initialized”例外.有人有什么想法? system.webServer modules runAllMana
我使用下面的方法来处理我的网站上的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)

解决方法

我遇到了同样的问题,但还没有找到解决方案.我已经确认问题是由响应上的StatusCode或Status属性设置引起的,它会影响IIS 7和IIS Express,但不会影响Cassini,这对于堆栈跟踪是有意义的.

我会继续调查.

编辑:没有找到解决方案的运气.我在IIS论坛上发布了同样的问题:http://forums.iis.net/p/1187959/2016914.aspx#2016914

编辑2:在.NET 4.5 RC中确认为已修复.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读