How to solve the problem that ASP.NET setting 404 page returns 302HTTP status code
This article introduces the knowledge of "how to solve the problem of how to return the 302HTTP status code on the ASP.NET setting page". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The configuration 404 page in the configuration file is as follows:
The copy code is as follows:
When visiting the website, the error page can be displayed normally, but the HTTP status code is 302, which is very unfriendly to SEO. Follow these steps to modify the error page to return the correct SEO-friendly status code:
1. Add the code to 404.aspx:
Response.Status = "404 Moved Permanently"
If you do not do pseudo-static, or do not add script mapping, the above is no problem, there is no need to read on. If you do pseudo-static, the status code returned by the 404 page is still 302, see step 2.
2. Add the following code to Global.asax:
The copy code is as follows:
Protected void Application_Error (object sender, EventArgs e)
{
/ / Code that runs when an unhandled error occurs
This.FileNotFound_Error ()
}
/ / /
/ 404 error handling
/ / /
Private void FileNotFound_Error ()
{
HttpException erroy = Server.GetLastError () as HttpException
If (erroy! = null & & erroy.GetHttpCode ())
{
Server.ClearError ()
String path = "~ / 404.aspx"
Server.Transfer (path)
/ / Context.Handler = PageParser.GetCompiledPageInstance (path, Server.MapPath (path), Context)
}
}
This is the end of the content of "how to solve the problem of ASP.NET setting 404 page returning 302HTTP status code". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!