Serving 404 Errors Correctly in ASP.Net

Posted by (JavaScript must be enabled to view this email address) on Tue 30 Sep 2008

You’ve developed a brand new site. It’s great; there is lots of good content; it’s standards compliant; And as it’s in .Net you have used the customErrorPages features of the framework. Brilliant. So why does your error page get indexed when you delete files from the site?

The .Net framework detects that your page is no longer there and so sends an HTTP Status Code 302 to the browser along with a redirect to the error page. The error page is then successfully served within the framework and as such sends an HTTP Status Code 200 to the browser. A user may well be able to read the text on the screen that says their file was not found but the bot crawling the site will think the page is still available as it received an HTTP Status Code 200.

To resolve this you need to do two things:

     
  1. Check the page exists - if it doesn’t server the error page.
  2.  
  3. Send the correct error code to the browser.

Check the page exists

In your global.asax you need to check the request is a valid one so within the application_BeginRequest code block add the following:

protected void Application_BeginRequest(object sender, EventArgs e)
{
//get the url
string url = Request.RawUrl;
//check the document exists on the file system
if ((!System.IO.File.Exists(Server.MapPath(url))))
{
//if the document does not exist serve the error page
Server.Transfer("/Error/FileNotFound.aspx");
}
}

Send the correct error status

Now that you can tell whether your page exists you can send the correct status, in your error page page_load event simply add:

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

Your Comments

  1. A user may well be able to read the text on the screen that says their file was not found but the bot crawling the site will think the page is still available as it received an HTTP Status Code 200.
    moncler
    moncler sale
    moncler coats
    moncler Jackets
    moncler store
    moncler uk
    Replica Watches
    Omega Replica
    Replica Rolex Watches
    Best Replica Watches
    Swiss Replica Watches

    Posted by Mauro Masucci on 11/04 at 07:58 AM

  2. great; there is lots of good content; it¨s standards compliant; And as it¨s in .Net you have used the customE
    Need evening dresses,or gowns? Formal evening dresses from dresses shop, evening gowns from dress4sale,Laundry and dress4sale
    Wedding dresses
    evening dresses
    evening dresses sale
    Dresses sale
    Wide collection of prom dresses, evening dresses and gowns, cocktail dresses, summer dress, 2010 summer dresses,
    little black dresses, beaded dresses
    Abercrombie & Fitch
    Abercrombie
    Abercrombie & Fitch clothes
    replica watches
    Abercrombie Fitch
    Abercrombie Fitch UKrrorPages features of the framework. Brilliant. So why does your error

    Posted by Mauro Masucci on 11/06 at 12:13 PM

  3. for Credit Counseling, a replica bag a financial education and counseling service based iwc portuguese based in Silver Spring, Md.The parent-child relationship <h2>coco chanel clothing</h2> relationship generally “has
    the parent being in <h4>christian louboutin pumps</h4> in charge and child being obedient,” says replica omega watches says Theodore Connolly, author of personal finance armani watches finance book “Road
    Out of Debt.“Co-signing, he burberry tote bag he says, unwittingly creates a master-servant situation, ladies armani watch situation, and the person that owes the <h3>gucci boots</h3> the money falls
    into a different status. シャネルストラップ status.  “A child lending to a <h2>handbag replicas</h2> a parent is almost reversing the relationship fake chanel bags relationship and could
    cause problems,” he adds.A <u>seamaster omega</u> adds.A few experts were split about how dior shoes how to seal the deal, but most <h1>tag heuer monaco replica</h1> most agree that

    Posted by Mauro Masucci on 11/19 at 04:08 AM

  4. MP5 Player - Wholesale MP5 Players - Cheap MP5 Video Player
    MP5 Player
    MP4 Player
    Wholesale Electronics
    Mobile Phone

    Posted by Mauro Masucci on 11/20 at 03:47 AM

  5. Posted by Mauro Masucci on 11/21 at 08:28 AM

  6. Posted by Mauro Masucci on 12/20 at 09:05 AM

  7. Party Dresses Beach Wedding Dresses White Wedding Dresses mother of the bride formal gowns, apparel, outfits in Austin, Texas. Discount modest mother of the groom dress in petite Informal Wedding Dresses Classic Wedding Dresses Fashion Wedding Dresses Elegant Wedding Dresses Sexy Wedding Dresses ordered 4 dresses to be used as bridesmaid dresses in a wedding. I have just received them and they are more beautiful than I imagined. The dresses were made with great skill and craftma Sweet Wedding Dresses Colored Wedding Dresses Red Wedding Dresses Wedding Dresses With Embellishment What fabric is this dress made Silk Wedding Dresses

    Posted by Mauro Masucci on 12/25 at 02:30 AM

Commenting is not available in this section entry.
← Back to Blog Homepage

About our Blog

Brantas Limited specialise in Dynamics CRM, SharePoint and System Integration using the Microsoft Platform. We are all experienced developers in various fields with our own specialities complementing those of our team.

We have been working with SharePoint since 2003, including Installation and Administration, Migration, Development and Support.

Related Solutions

    No Related Posts Found

Related Case Studies

    No Related Posts Found