This article is a mirror article of machine translation, please click here to jump to the original article.

View: 17912|Reply: 2

[Source] asp.net Customize 404 in MVC4

[Copy link]
Posted on 3/22/2019 10:20:59 AM | | | |
There are certainly many ways to define a 404. Different methods take different forms and experience them differently. There are 2 types of information below

Method 1:

1. Find the node "system.web" xx... xx〈System.web〉 enables the 404 configuration

2. Define a controllers Error (this is up to you), in action as follows

public ActionResult Index()
{
Response.Status = "404 Not Found";
Response.StatusCode = 404;
return View();
}
This defaults to adding ?aspxerrorpath=/ eg:http://localhost/Error?? aspxerrorpath=/123456 Therefore, it is not recommended to try it

Method 2:

Open the Global.asax file
Defining Error Direction Addresses (Controller/Action)


Added a corresponding controller method


Call it a day.. Of course, configuring this is the final step in the end of the project. Otherwise, you won't see any effect when you debug.





Previous:IPv4 and IPv6 address lengths
Next:Automatic input with a handle cannot be clicked to confirm the operation
 Landlord| Posted on 3/22/2019 10:22:52 AM |
According to the configuration of method 2, there is no problem in testing locally, and there will be problems when deployed to the official environment IIS, or the 404 page of IIS is displayed, as shown in the figure below:


It is said that Response.StatusCode = 404 is removed; It's okay to remove it
 Landlord| Posted on 3/22/2019 10:38:49 AM |
The final solution is as follows:


web.config configuration filesystem.webServerThe following nodes have been added:

Controller Code:



For non-existent paths, the 404 status code can be correctly corresponded to which is beneficial for SEO.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com