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

View: 15179|Reply: 0

[ASP.NET] Asp.Net 404 page setup

[Copy link]
Posted on 12/24/2015 5:39:45 PM | | | |


What is a 404 error?
HTTP 404 error means that the web page to which the link points does not exist, that is, the URL of the original web page is invalid, which often happens and is difficult to avoid, such as: changes in the web page URL generation rules, renaming or moving the web page file, misspelling of the imported link, etc., resulting in the original URL address being inaccessible; When a web server receives a similar request, it returns a 404 status code telling the browser that the resource to be requested does not exist. However, the default 404 error page for web servers, whether Apache or IIS, is very rudimentary, dull, and unuser-friendly, and does not provide users with the necessary information to get more clues, which will undoubtedly cause user churn.
The role of 404 pages
Search engines use HTTP status codes to identify the status of a web page. When a search engine gets an incorrect link, the website should return a 404 status code telling the search engine to abandon indexing the link. If a 200 or 302 status code is returned, search engines index the link, resulting in a large number of different links pointing to the same web content. As a result, search engines trust the site significantly less.
How to check that a custom 404 page returns a "404" status code
After setting up the custom 404 error page, be sure to check whether it correctly returns the "404" status code. The method of checking is also quite simple, enter the URL of a website that does not exist in the web page, check the return of the HTTP header, and be sure that it returns the "404" status code.
404 page is the right thing to do
1. How to set up the Apache server 404 page
Add the code to the .htaccess file: ErrorDocument 404/Error.html
Build a simple HTML404 page naming Error.html
Place Error.html in the root directory of your website.
2. Set the 404 error page under IIS/ASP.net
First, modify the settings of the root directory of the application, open the "web.config" file edit, and add the following content to it:

<configuration>
<system.web>
<customErrors mode=”On” defaultRedirect=”error.asp”>
<error statusCode=”404″ redirect=”notfound.asp” />
</customErrors>
</system.web>
</configuration>

In this example, error.asp is the default 404 page and notfound.asp is a customized 404 page.
Then, add the custom 404 page "notfound.asp":

<%
Response.Status = “404 Not Found”
%>

This ensures that IIS returns the "404" status code correctly
Friendly reminder
1. Be sure not to direct the 404 error directly to the homepage of the website, which may cause your homepage to not be included;
2. /Error.html Please do not bring the main domain name in front of it (wrong spelling: yzznl.cn/error.html, correct writing :/error.html), otherwise the status code returned is 302 or 200 status code.
When searching for how to customize a 404 error page from asp.net website, most articles tell you to go to web.config, find system.web, and configure the following.

Copy code The code is as follows:

<customErrors mode="On" error statusCode="404" redirect="/404.htm" >
</customErrors>


In fact, after doing this, you will not get the header information of the 404 unfound web page, but the header information of the 302 temporary redirect. You can use the Check Headers Tool tool to check the HTTP Status Codes.

So how to properly configure custom 404 error web pages for asp.net website?

Start by making a 404.aspx page, then configure it in customeErrors under the web.config file as follows:
customErrors redirectMode="ResponseRewrite"
error statusCode="404" redirect="~/404.aspx"/
/customErrors

RedirectMode has two properties.
ResponseRedirect refers to redirecting a user to that error page, and the original URL is changed to the URL of that error page.
ResponseRewrite refers to directing the user to the error page without changing the original URL in the browser.

Then in the 404.aspx.cs file, add the following code for the Page_Load event:
Copy code The code is as follows:

protected void Page_Load(object sender, EventArgs e)
{
Response.Status = "404 Not Found";
}





Previous:aspx vs. mvc page captcha
Next:c# SmtpClient sends the email source code
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