Giancarlo Sierra Giancarlo Sierra - 1 year ago 242
ASP.NET (C#) Question

Custom error page for Http error 404.13 ASP.NET Core MVC

I am fairly new to ASP.NET and MVC in general; I have been migrating an ASP.NET MVC app to ASP.NET MVC Core. In the former framework I was able to handle an HttpException for the following infamous error:


HTTP Error 404.13 - Not Found

The request filtering module is configured to deny a request that
exceeds the request content length.


I am aware I can increase the maximum upload allowed length, which currently is 30MB by default, but my objective is to present the user with a friendly error page explaining what just happened, not increase the allowed limit.

On ASP.NET I did this with the following code on my Global.asax :

private void Application_Error(object sender, EventArgs e)
{
var ex = Server.GetLastError();
var httpException = ex as HttpException ?? ex.InnerException as HttpException;
if (httpException == null) return;

if (httpException.WebEventCode == WebEventCodes.RuntimeErrorPostTooLarge)
{
//handle the error
Response.Redirect("~/Error/UploadTooLarge"); //Redirect to my custom error page

}
}


I can't seem to find an alternative for this in Asp.Net Core after several hours of research.
I believe I will need to plug in some Middleware in my Startup.cs Configure method to achieve a custom error page to handle the HttpException and redirect it but I am truly lost on this matter.

I have managed to successfully use the following middleware for custom error pages for http errors such as 404 - Not Found or 403 - Forbiden by using the following on my configure method :

app.UseStatusCodePagesWithReExecute("/Error/StatusCode{0}");


Alongside a controller:

public class ErrorController : Controller
{
public IActionResult StatusCode404()
{
return View(viewName: "CustomNotFound");
}

public IActionResult StatusCode403()
{
return View("CustomForbiden");
}
}


And the corresponding views. However, the 404.13 error (upload too large) wont be handled by my current middleware. I believe IIS is presenting the error as it is not handled by the web app.

Answer Source

You are correct. IIS is nabbing the error before it gets into your pipeline. I would recommend adding the httpErrors module into your web.config and pointing it at a page on the site.

<system.webServer>
  <httpErrors errorMode="Custom" existingResponse="Replace">
    <remove statusCode="404" subStatusCode="13" />
    <error statusCode="404"
           subStatusCode="13"
           prefixLanguageFilePath=""
           path="http://yourwebsite.com/path/to/page"
           responseMode="Redirect" />
  </httpErrors>
</system.webServer>