Can ASP.NET MVC Area display its own set of error pages?

I have combined some of my web applications into one of the main ASP.NET MVC projects; assigning some of them to separate areas so that they can be accessed through subdomains.

Using this (very useful) resource ( https://dusted.codes/demystifying-aspnet-mvc-5-error-pages-and-error-logging ), I set customErrors and httpErrors to Web.config so that user pages are displayed mistakes. It works well.

I use different layouts / styles for the region / subdomain, so I wonder: How can I get Area to display my own set of error pages?

In the current configuration, all subdomains will display the main set of user errors that are added to the customErrors and httpErrors (403.html, 404.html, etc.); but I would prefer to set up error pages for some subdomains. (If, for example, one of the areas is handled exclusively by a separate domain, for example, it would be inappropriate to serve regular error pages.)

Update: Here is a script, with code, on request. Thanks to Ben Foster, who suggested good recommendations here: http://benfoster.io/blog/aspnet-mvc-custom-error-pages 2 . I put the code for customErrors, but not the corresponding httpErrors ... left it for brevity.

  <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> 

The above code works well:

  • If I go to "example.com/does/not/exist", I get the expected error page ~/404.aspx .
  • If I go to "example.com/MyArea1/does/not/exist", I will get a user error page ~/Areas/MyArea1/404.aspx .

Task:

Now I want the area (MyArea2) to be served by a completely separate domain (e.g. exampleOnMyOtherDomain.com) using HostDomainConstraint (as recommended by @TetsuyaYamamoto, in the comment below). A link will now be available that would be accessible via "example.com/MyArea2/validlink": "exampleOnMyOtherDomain.com/validlink".

Now, if I try "exampleOnMyOtherDomain.com/does/not/exist", I will serve 404 top level ( ~/404.aspx ). This is probably because "MyArea2" is no longer in the path, so the location using the "MyArea2" path will not be found.

How can I get Area (MyArea2) to serve my own error pages?

+8
asp.net-mvc web-config custom-errors
source share
1 answer

Based on some research and helpful pointers from @TetsuyaYamamoto, I used the following strategy.

I am handling the Application_Error event in Global.asax using the following code:

 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(); } } 

Note:

  • hostName.Contains("exampleOnMyOtherDomain") should compare the host name with the area that interests me. I will add else...if expressions for other areas;
  • Response.TrySkipIisCustomErrors = true should prevent IIS from trying to handle the error;
  • Response.StatusCode correctly sets the status code ("404", "500" ...);
  • Server.Transfer() is read in a file that I would like to display as an error page;
  • Server.ClearError() should signal that the error has already been processed.

I want customErrors / httpErrors to continue to handle regular errors. When execution passes through the Application_Error block without processing (i.e. Server.ClearError() not called), customErrors / httpErrors will handle the error.

This seems to be a decent error handling strategy for areas served by another domain.

+6
source share

All Articles