Monday, September 30, 2013

Create Your Own 404 Custom Error Page in ASP.NET


Step #1

Add a page to your website with the name 404.aspx. Style your page and put whatever content you want on this page.


Step #2

Add the following code to your Web.config file:

<configuration>
  <system.web>
    <!--For files-->
    <customErrors mode="On" redirectMode="ResponseRewrite">
      <error statusCode="404" redirect="/404.aspx"/>
    </customErrors>
  </system.web>

  <system.webServer>
    <!--For Directories-->
    <httpErrors errorMode="Custom">
      <remove statusCode="404"/> <!-- This is needed to remove the defautl error -->
      <error statusCode="404" path="/404.aspx" responseMode="ExecuteURL"/>
    </httpErrors>
  </system.webServer>
</configuration>

Step #3

Add the following code to your code behind page.

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

This code will make the page respond with a 404 status code. If you don't do this, you will get code 200 which means OK. Bots and crawlers don't have eyes to see your content. They only care about the status code. If your status code says 404, then they will know it is a page not found.

No comments:

Post a Comment