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

I'm new to ASP.NET MVC and in general; I endured the ASP.NET MVC application in ASP.NET MVC Core. In the former structure, I was able to handle HttpException for the following infamous error:

HTTP 404.13 error - not found

filtering module is configured to request rejection of the request that exceeds the request content length.

I know that I can increase the maximum allowable load length, which by default is 30 MB, but my goal - to introduce user friendly error page explaining what had just happened, and not to increase the limit.

In ASP.NET, I did it with the following code to 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 not find alternatives to this in Asp.Net Core after a few hours of research. I believe that I will need to connect an intermediate software in my method Startup.cs Configure to create a custom error page to handle exceptions HttpException and redirect, but I really lost this question.

I was able to successfully use the following middleware software for custom error pages for HTTP-errors, such as 404 - Not Found or 403 - Forbiden, using the following method in my configure:

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

In addition to the controller:

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

And appropriate representation. However, the error 404.13 (load is too large) will not be processed my current middleware. I believe that IIS is a mistake, because it is not processed by the web application.

+6
source share
2 answers

You're right. IIS throws an error before it gets into your pipeline. I would recommend to add the module httpErrors in your web.config and specify the 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> = "Replace"> <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> 
+2
source

Old post, but it is still relevant. My project Core 2.2 MVC, which includes loading of large stream files needed delicate handling outcome 404.13 (Request entity too large). The usual way of handling code setting state (graceful presentation) is in Startup.cs Configure () plus the action method to meet:

 app.UseStatusCodePagesWithReExecute("/Error/Error", "?statusCode={0}"); 

and

 public IActionResult Error(int? statusCode = null) { if (statusCode.HasValue) { Log.Error($"Error statusCode: {statusCode}"); if (statusCode == 403) { return View(nameof(AccessDenied)); } if (statusCode == 404) { return View(nameof(PageNotFound)); } } return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } null) public IActionResult Error(int? statusCode = null) { if (statusCode.HasValue) { Log.Error($"Error statusCode: {statusCode}"); if (statusCode == 403) { return View(nameof(AccessDenied)); } if (statusCode == 404) { return View(nameof(PageNotFound)); } } return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } statusCode}"); public IActionResult Error(int? statusCode = null) { if (statusCode.HasValue) { Log.Error($"Error statusCode: {statusCode}"); if (statusCode == 403) { return View(nameof(AccessDenied)); } if (statusCode == 404) { return View(nameof(PageNotFound)); } } return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } ; public IActionResult Error(int? statusCode = null) { if (statusCode.HasValue) { Log.Error($"Error statusCode: {statusCode}"); if (statusCode == 403) { return View(nameof(AccessDenied)); } if (statusCode == 404) { return View(nameof(PageNotFound)); } } return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } ; public IActionResult Error(int? statusCode = null) { if (statusCode.HasValue) { Log.Error($"Error statusCode: {statusCode}"); if (statusCode == 403) { return View(nameof(AccessDenied)); } if (statusCode == 404) { return View(nameof(PageNotFound)); } } return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } 

But since the error is processed 404.13 IIS, but not in the pipeline MVC, the above code does not allow to set an elegant presentation of the error "too big load." To do this I had to hold your nose and add the following web.config file in my project Core 2.2. Please note that removal of 404.13 also removed 404 so ErrorController code () is no longer processing 404, therefore, two custom error handler are listed below. Hope this helps someone!

 <?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <security> <requestFiltering> <!-- This will handle requests up to 201Mb --> <requestLimits maxAllowedContentLength="210763776" /> </requestFiltering> </security> <httpErrors errorMode="Custom" existingResponse="Replace"> <remove statusCode="404" subStatusCode="13" /> <remove statusCode="404" /> <error statusCode="404" subStatusCode="13" prefixLanguageFilePath="" path="/Error/UploadTooLarge" responseMode="Redirect" /> <error statusCode="404" prefixLanguageFilePath="" path="/Error/PageNotFound" responseMode="Redirect" /> </httpErrors> </system.webServer> </configuration> to 201Mb -> <?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <security> <requestFiltering> <!-- This will handle requests up to 201Mb --> <requestLimits maxAllowedContentLength="210763776" /> </requestFiltering> </security> <httpErrors errorMode="Custom" existingResponse="Replace"> <remove statusCode="404" subStatusCode="13" /> <remove statusCode="404" /> <error statusCode="404" subStatusCode="13" prefixLanguageFilePath="" path="/Error/UploadTooLarge" responseMode="Redirect" /> <error statusCode="404" prefixLanguageFilePath="" path="/Error/PageNotFound" responseMode="Redirect" /> </httpErrors> </system.webServer> </configuration> = "Replace"> <?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <security> <requestFiltering> <!-- This will handle requests up to 201Mb --> <requestLimits maxAllowedContentLength="210763776" /> </requestFiltering> </security> <httpErrors errorMode="Custom" existingResponse="Replace"> <remove statusCode="404" subStatusCode="13" /> <remove statusCode="404" /> <error statusCode="404" subStatusCode="13" prefixLanguageFilePath="" path="/Error/UploadTooLarge" responseMode="Redirect" /> <error statusCode="404" prefixLanguageFilePath="" path="/Error/PageNotFound" responseMode="Redirect" /> </httpErrors> </system.webServer> </configuration> 
0
source

All Articles