How to avoid custom/Server error in web site?

I have asp.net web application located on server I want to avoid all custom and server error from my site.

For that I have used

<customErrors mode="RemoteOnly" defaultRedirect="~/ErrorPage/TryAgainLater.aspx">   <error redirect="~/ErrorPage/PageNotFound.aspx" statusCode="404"/> </customErrors>

Using above code will able avoid some issue. ie.

Suppose "http://Exaple.com/Careers.aspx" Page available in my site then

Case 1. http://Exaple.com/Careersss.aspx "It was working correct as per above rule".

Case 2. http://Exaple.com/[email protected]##Careersss.aspx "Not working" Note : Here I add special character

Case 3: http://Exaple.com/Careersss.aspxxxx "Not working" Note : add character after ".aspx"

case 4: http://Exaple.com/Careersss.aspx/[email protected][email protected][email protected]! "Not works design breaking here". Note : Add '/' with special character.

please help me when user get case 2,3,4 then they automatically redirected to error page.

Thanks In advance.

Answers:

Answer

If you don't want to change IIS settings, you can install your own handler for 404 errors in Global.asax. Just add this method to our Global.asax.cs code-behind file:

protected void Application_EndRequest(object sender, EventArgs e)
{
    HttpResponse response = HttpContext.Current.Response;
    if (response.StatusCode == 404 && response.SubStatusCode == 0)
    {
        response.Redirect("/test/TryAgainLater.aspx?error=NotFound");
        response.End();
    }
}

If it does not fix the issue and you are still getting 404 error page, add this setting to your web.config file:

<configuration>
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
</configuration>

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.