Yesterday the company asked for all items to add a custom 404 error page, the specific requirements to achieve the following points:
1. Implement custom errors (such as various error,404, etc.) to jump to the specified page
2. The HTTP status value for the specified page output must be 404 or another specified statecode
3. Jump to the custom error page must be. html suffix end
On the 1th, this is very simple, we all know, only need to webconfig inside the customerrors node to set the corresponding, as follows
<customerrors mode= "on" defaultredirect= "error.html" > <error statuscode= "404" redirect= " 404notfond.html "></error> </customErrors>
Because the real HTML static page output HTTP status value can only be (OK), so, in order to make the output of StatusCode 404 or other values, it must be pseudo-static, in MVC, an HTML page to achieve pseudo-static effect is also very simple, only need to do some configuration of the route, In the Global.aspx.cs file.
///<summary>////</summary>/// <param name= "routes" ></param> public static void RegisterRoutes (RouteCollection routes) { Routes. Ignoreroute ("{resource}.axd/{*pathinfo}"); Routes. Ignoreroute ("{resource}.jpg"); #region error page route routes. MapRoute ("404NotFund",//Route name "404notfond.html",//URL with parameters New {controller = "error", action = "Error"}//Pa Rameter defaults); Routes. MapRoute ("Error",//Route name "error.html",//URL with parameters new {controller = "error", action = "Error"}//Parameter de faults); #endregion}
Here, we will route the two custom error pages to the action named error under Errorcontroller, and then you need to tell the action to output the StatusCode
public class Errorcontroller:controller {//<summary>///Page Error//</summary>// < Returns></returns> Public ActionResult error () { response.status = ' 404 Not Found '; Response.statuscode = 404; return View ();} }
Here, we specify that the output of statuscode is 404, and then you can whitewash your real 404 custom error page, which is the page of the action output views/error/error.aspx
After all this is done, you can start debugging locally to see the effect, but before you debug, be sure to delete the error.html and 404notfond.html pages in your project, because, in the MVC default settings, If the static file in the Access path URL exists in the project and it takes precedence over the static file, bypassing the route, then your global settings for these static file routes will be useless
Make sure that you do not have a static file with the same name on the same path in your project and that you are debugging locally, you should be able to achieve the desired effect.
However, when I deployed the project to the server's IIS7, I found that on the local access server, always came out is
It's not the custom error page I specified, but it's normal for the local debugger and the browser on the server to open it, so I suspect IIS is struggling through the afternoon and I finally find a key point in the IIS site settings for the "error page" setting
Right-click 404--edit Function settings
Click to open
The default setting is "Verbose errors for local requests and custom errors for remote requests", which means that if you open it locally (open in the server with a browser) it shows a verbose error, if it is a remote request (extranet access server) it shows a custom error page, and it (IIS) understands that the custom error page is
That way, you understand, and you know what to do.
There are two options, one is the "Edit error page settings" Panel is set to always show "details", the second is the "Edit custom error page" Panel to modify the file path to your custom HTML error page
Either option can achieve the desired effect
This blog post for my original, to reprint, please indicate the source ~
MVC custom error page 404 static page