Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that ASP.NET setting 404 page returns 302HTTP status code

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report