Handle 404 errors in web projects

Source: Internet
Author: User

When developing a web project, we often encounter a situation where the data in the database does not exist or is deleted, and the corresponding web page does not exist/error. So how should we deal with this situation?

Generally, similar code: Product pro = GetProduct (123); returns the Product information numbered 123. If you do not judge pro = null, you can directly reference the property of pro, the NullReferenceException (to reference an object to an object instance) is thrown. set in config

<CustomErrors mode = "RemoteOnly" defaultRedirect = "err. aspx">

<Error statusCode = "403" redirect = "NoAccess.htm"/>

<Error statusCode = "404" redirect = "404. aspx"/>

</CustomErrors>

 

Will go to the Progress page, but the jump to err. aspx is the 302 status code returned. In fact, because the product does not exist, we need to return the status code 404. The Status Code 404 is not only for the user, but for the user, it does not matter, however, the search engine will see this status code. If a large number of products do not exist, this will lead to the existence of the product information previously indexed by the search engine, the actual content is not found, which will reduce the search engine's trust in the website. See my practices below:

Create 404. aspx in the root directory of the website. The background code is as follows:

 

Protectedvoid Page_Load (object sender, EventArgs e)

{

Response. StatusCode = 404;

}

 

The front-end code is as follows:

 

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "404. aspx. cs" Inherits = "Web. _ 04" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">

<Head runat = "server">

<Title> the page you accessed does not exist </title>

</Head>

<Body>

<Form id = "form1" runat = "server">

<Div>

<H1> the page you visit does not exist (will automatically jump to the <a href = "http://www.svnhost.cn"> Home page </a>)
<Script language = "javascript">

Function go ()

{

Window. location = "http://www.svnhost.cn ";

}

SetTimeout (go, 3000 );

</Script>

</Div>

</Form>

</Body>

</Html>

Now we have a 404 page.

Next we will call this page:

Product pro = GetProduct (123 );
If (pro = null) Server. Transfer ("/404. aspx ");

After deleting a product, the search engine crawls the page and returns the 404 status code. Then, the index is deleted.

If the website has a similar problem, you can view it in the Google website administrator tool.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.