Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Hi,

I need to redirect my site to it's default page, if anybody enter an invalid url to address bar within my site.
Which rule i need to create in Global.asax
Posted

protected void Application_Error(object sender, EventArgs e) 
    { 
        Exception exception = Server.GetLastError(); 
        Response.Clear(); 
 
        HttpException httpException = exception as HttpException; 
 
        if (httpException != null) 
        { 
             
            switch (httpException.GetHttpCode()) 
            { 
                case 404: 
                    // page not found 
                   Response.Redirect("default.aspx");
                    break; 
                case 500: 
                    // server error 
                    routeData.Values.Add("action", "HttpError500"); 
                    break; 
                default: 
                    routeData.Values.Add("action", "General"); 
                    break; 
            } 
 
          
 
            // clear error on server 
            Server.ClearError(); 
 
                   } 
    } 



Add the above code in global.asax in application error event block.

You can also refer below link for more details and different methods to implement this
Error Handling in ASP.NET[^]


Hope this will help you out!!!
 
Share this answer
 
v3
Comments
[no name] 27-Sep-11 9:14am    
Thanks, i changed little bit and got the solution.
sujit0761 27-Sep-11 9:16am    
great.. it was just an example...

Anyways..Happy coding!!!
You don't need to - you can do it via the web.config in your root directory.
Add to your <system.web> section:
XML
<customErrors defaultRedirect="~/Whoops.aspx" mode="Off" redirectMode="ResponseRewrite">
  <error statusCode="403" redirect="~/NoAccess.aspx"/>
  <error statusCode="404" redirect="~/NotFound.aspx"/>
</customErrors>
 
Share this answer
 
Comments
[no name] 27-Sep-11 9:13am    
it's work. thank you.
Have a look at this article => Smart 404 error handling[^]
 
Share this answer
 
XML
<system.web>
    <httpRuntime maxRequestLength="102400"/>
    <customErrors mode="RemoteOnly" defaultRedirect="Default.aspx"/>

        <!--
            Set compilation debug="true" to insert debugging
            symbols into the compiled page. Because this
            affects performance, set this value to true only
            during development.
        -->
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900