Friday, March 1, 2013

How to Redirect from Non-Exist url to a Existing One C#

Hello Everyone today I’m gonna teach you, if the user type a URL that doesn’t even exist, so how to rediect them to a Existing Page without showing them the default 404 Error Page.
We will use the web.config file for this purpose, this can also be done in IIS via URLReWrite

1. First Add 2 webpage and name them home.aspx and Error.aspx
2. Now open your web.config
3. Find <System.Web> tag
4.  Now place the following code inside the <System.Web> tag

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

This code will check the url , if the url exist than this code will do nothing, but if the page doesn’t exist it will rediect them to Error.aspx page.


  •  Error Code 404 means Page Not Found
  • You can Change the StatusCode according to your requirements
  • If CustomErrors mode is set to Off than this rule will not do anything
Browser StatusCodes 
  • 1xx Informational
  • 2xx Success
  • 3xx Redirection
  • 4xx Client Error
  • 5xx Server Error
I hope this was informative for you and I would like to thank you for reading. If you still have any queries than do comment.

No comments:

Post a Comment