ASP.NET MVC区域可以显示自己的错误页面集吗?

cyr*_*llo 9 asp.net-mvc web-config custom-errors

我一直在将我的一些Web应用程序整合到一个主要的ASP.NET MVC项目中; 将其中一些分配给单独的区域,以便可以通过子域访问它们.

使用这个(很有用)资源(https://dusted.codes/demystifying-aspnet-mvc-5-error-pages-and-error-logging),我已经设置了customErrorshttpErrorsWeb.config,使自定义错误页面显示.效果很好.

我将在Area/subdomain中使用不同的布局/样式,所以我想知道: 如何让Area显示自己的错误页面?

使用当前设置,所有子域都将显示添加到customErrorshttpErrors部分的主要自定义错误集(403.html,404.html等); 但我更喜欢为某些子域定制错误页面.(例如,如果其中一个区域完全由一个单独的域处理,那么提供常规错误页面是不切实际的.)

更新: 这是根据请求使用代码的方案.感谢Ben Foster,他在这里提供了很好的指导:http://benfoster.io/blog/aspnet-mvc-custom-error-pages 2.我已经为customErrors设置了代码,但是没有相应的httpErrors ...为了简洁起见.

  <system.web>
    <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/500.aspx">
      <error statusCode="404" redirect="~/404.aspx" />
      <error statusCode="500" redirect="~/500.aspx" />
    </customErrors>
  </system.web>

  <location path="MyArea1">
    <system.web>
      <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Areas/MyArea1/500.aspx">
        <error statusCode="404" redirect="~/Areas/MyArea1/404.aspx" />
        <error statusCode="500" redirect="~/Areas/MyArea1/500.aspx" />
      </customErrors>
    </system.web>
  </location>

  <location path="MyArea2">
    <system.web>
      <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Areas/MyArea2/500.aspx">
        <error statusCode="404" redirect="~/Areas/MyArea2/404.aspx" />
        <error statusCode="500" redirect="~/Areas/MyArea2/500.aspx" />
      </customErrors>
    </system.web>
  </location>
Run Code Online (Sandbox Code Playgroud)

上面的代码效果很好:

  • 如果我导航到"example.com/does/not/exist",我会收到预期的错误页面~/404.aspx.
  • 如果我导航到"example.com/MyArea1/does/not/exist",我将获得自定义错误页面~/Areas/MyArea1/404.aspx.

挑战:

现在我想要一个区域(MyArea2)由一个完全独立的域(例如exampleOnMyOtherDomain.com)提供服务(使用HostDomainConstraint@TetsuyaYamamoto推荐,在下面的评论中).现在将通过"example.com/MyArea2/validlink"访问可以访问的链接:"exampleOnMyOtherDomain.com/validlink".

现在,如果我尝试"exampleOnMyOtherDomain.com/does/not/exist",我将获得顶级404(~/404.aspx).这可能是因为"MyArea2"不再位于路径中,因此将不会拾取路径为"MyArea2"的位置.

如何让Area(MyArea2)提供自己的错误页面?

cyr*_*llo 6

根据@TetsuyaYamamoto的一些研究和有用的指示,我采用了以下策略.

我正在Global.asax使用以下代码处理"Application_Error"事件:

protected void Application_Error(object sender, EventArgs e)
{
    string hostName = Request.Headers["host"].Split(':')[0];
    if (hostName.Contains("exampleOnMyOtherDomain"))
    {
        Exception exception = Server.GetLastError();
        Response.Clear();
        HttpException httpException = exception as HttpException;
        Response.TrySkipIisCustomErrors = true;

        switch (httpException.GetHttpCode())
        {
            case 404:
                Response.StatusCode = 404;
                Server.Transfer("~/Errors/MyArea2_404.htm");
                break;
            case 500:
            default:
                Response.StatusCode = 500;
                Server.Transfer("~/Errors/MyArea2_500.htm");
                break;
        }
        Server.ClearError();
    }
}
Run Code Online (Sandbox Code Playgroud)

注意事项:

  • hostName.Contains("exampleOnMyOtherDomain")应该将主机名与我感兴趣的区域进行比较.我会else...if为其他地区添加陈述;
  • Response.TrySkipIisCustomErrors = true 应该阻止IIS尝试处理错误;
  • Response.StatusCode 适当设置状态代码('404','500'......);
  • Server.Transfer() 读入我想显示为错误页面的文件;
  • Server.ClearError() 应该表明错误已经处理完毕.

我想customErrors/ httpErrors继续处理常规错误.当执行通过Application_Error块而没有被处理(即Server.ClearError()没有被调用)时,customErrors/ httpErrors将处理错误.

对于处理由不同域服务的区域的错误,这似乎是一个不错的策略.