Skip to content

Instantly share code, notes, and snippets.

var exception = Server.GetLastError();
<customErrors>
<error statusCode="500" redirect="~/path-to-your-error-page" />
</customErrors>
<customErrors defaultRedirect="~/path-to-your-error-page" />
<system.web>
<customErrors defaultRedirect="" />
<!-- any other elements -->
</system.web>
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new {controller="Home", action="Index", id=UrlParameter.Optional});
// More routes here.
public class SomeApplicationClass : HttpApplication
{
...
}
Response.ClearHeaders();
Response.ClearContent();
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
Response.TrySkipIisCustomErrors = true;
<system.webServer>
...
<httpErrors errorMode="Custom" existingResponse="Auto">
<remove statusCode="403" subStatusCode="14"/>
<error statusCode="403" subStatusCode="14"
responseMode="ExecuteURL" path="/path/to/Error/Forbidden"/>
<remove statusCode="404"/>
<error statusCode="404" responseMode="ExecuteURL"
path="/path/to/Error/NotFound"/>
</httpErrors>
<system.web>
...
<customErrors mode="On"/>
</system.web>
Response.ClearHeaders();
Response.ClearContent();
Response.StatusCode = (int)HttpStatusCode.InternalServerError;