Friday, May 17, 2013

How to handle 404 Server side error in .net

 

This server side error usually will be come, when we give the wrong URL. now how we can handle the in your .net application. That time we will show the custom error message. like this create one 404.aspx page in your own way to display that error message using HTML. Next this method is URLRewriter. we can get the error response then response is Empty you can redirect to 404.aspx page.
public void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;

if (HttpContext.Current.Request.Url.ToString().Contains("Redirect.aspx"))
{
if (context.Request.QueryString["key"] != null)
{
string inputStr = context.Request.QueryString["key"].ToString();

if (inputStr != "none/")
{
if (inputStr.EndsWith("/"))
{
inputStr = inputStr.Substring(0, inputStr.Length - 1);
}

HttpContext.Current.RewritePath(Helper.GetRedirectPage(inputStr), false);
}
}
else
{
HttpContext.Current.RewritePath("~/Server_Error/404.aspx", false);
}
}
}
and another one way to handle this error message. simply we can wrote in your web.config file like this...
<customErrors mode="On">
<error statusCode="403" redirect="NoAccess.htm"/>
<error statusCode="404" redirect="~/Server_Error/404.aspx"/>
</customErrors>

0 comments:

Post a Comment