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

cyrotello

I've been consolidating some of my web apps into one main ASP.NET MVC project; assigning some of them to separate Areas, so that they can be accessed via subdomains.

Using this (quite helpful) resource (https://dusted.codes/demystifying-aspnet-mvc-5-error-pages-and-error-logging), I've set up customErrors and httpErrors in the Web.config, so that custom error pages are displayed. Works well.

I'll use different layout/styling per Area/subdomain, so I'm wondering: How can I get an Area to display its own set of error pages?

With the current setup, all the subdomains will display the main set of custom errors that are added to the customErrors and httpErrors sections (403.html, 404.html, et cetera); but I'd prefer tailored error pages for some subdomains. (If one of the areas is exclusively handled by a separate domain altogether, for example, it won't be practical to serve the regular error pages.)

Update: Here is the scenario, with code, as requested. Thanks to Ben Foster, who offered good guidance here: http://benfoster.io/blog/aspnet-mvc-custom-error-pages2. I've put the code for customErrors, but not the corresponding httpErrors... left it out 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 navigate to "example.com/does/not/exist", I get the expected error page at ~/404.aspx.
  • If I navigate to "example.com/MyArea1/does/not/exist", I will get the custom error page at ~/Areas/MyArea1/404.aspx.

The challenge:

Now I'd like an Area (MyArea2) to be served by a totally separate domain (e.g. exampleOnMyOtherDomain.com), using HostDomainConstraint (as recommended by @TetsuyaYamamoto, in a comment below). A link that would have been accessible via "example.com/MyArea2/validlink" will now be accessed this way: "exampleOnMyOtherDomain.com/validlink".

Now, if I try "exampleOnMyOtherDomain.com/does/not/exist", I'll be served the top-level 404 (~/404.aspx). This is probably because "MyArea2" is not in the path anymore, so the location with the path "MyArea2" will not be picked up.

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

cyrotello

Based on some research, and helpful pointers from @TetsuyaYamamoto, I've employed the strategy below.

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

Points to note:

  • hostName.Contains("exampleOnMyOtherDomain") should compare the Host name with the Area I'm interested in handling. I'll add else...if statements for other Areas;
  • Response.TrySkipIisCustomErrors = true should prevent IIS from attempting to handle the error;
  • Response.StatusCode sets the status code appropriately ('404', '500'...);
  • Server.Transfer() reads in a file that I'd like to display as the error page;
  • Server.ClearError() should signal that the error has already been handled.

I want customErrors/httpErrors to continue handling regular errors. When execution passes through the Application_Error block without being handled (i.e. Server.ClearError() isn't called), customErrors/httpErrors will handle the error.

It seems that this is a decent strategy for handling errors for Areas that are served by a different domain.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Custom error pages in ASP.NET MVC

Asp .Net Core How to handle error pages in area

Getting error 404 not found with ASP.NET MVC Area routing

ASP .NET MVC Area registration

ASP.NET Core MVC 3.1 - Make controller unaccessible outside is own area

Custom error pages on asp.net MVC3

In ASP.NET MVC 5, how does the framework knows that a POST request comes from its own webpage?

How can a .NET assembly detect its own version within an IIS ASP.NET context?

How to create multiple pages with its own set of widgets in Shiny

ASP.NET MVC WebAPI in Application Area

asp.net mvc routing, with area as parameter

Set Page from Area Folder as default page in asp.net core 2.2 MVC

ASP.NET Core MVC (2.2) Error Middle-ware not triggering error pages

How to open a view form server site and display its link in Asp.net MVC?

Can I create an Area in an Existing ASP.NET MVC 5 App that uses Angular.js?

How can I get my own User in controller in Asp.net core mvc

Asp.Net MVC how can I make an own close and fullscreen button?

How to specify max-age for error pages in ASP.NET MVC

How to make custom error pages work in ASP.NET MVC 4

Wordpress DELETing pages ON ITS OWN

Add area in Asp.net MVC in vs code or .net cli

Error handling in ASP .Net MVC

JavaFX: Can't set items of TableView to FilteredList of its own items

Set simple URL route for Area in ASP MVC, without RoutArea

How can i find specific Image Control and set its ImageUrl property (ASP.net - vb)

Is it possible to display selected items in dropdowncheckboxes text area in asp.net

asp.net mvc viewdata display in a viewpage

Display uploaded image asp.net mvc

Asp.Net MVC Getting Display Names