Skip to content

Instantly share code, notes, and snippets.

@jsakamoto
Created October 10, 2012 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsakamoto/3862474 to your computer and use it in GitHub Desktop.
Save jsakamoto/3862474 to your computer and use it in GitHub Desktop.
Respond Real HTTP status code in custome error page
using System;
using System.Configuration;
using System.Web;
using System.Web.Configuration;
public class MvcApplication : System.Web.HttpApplication
{
public override void Init()
{
base.Init();
this.PreSendRequestHeaders += MvcApplication_PreSendRequestHeaders;
}
void MvcApplication_PreSendRequestHeaders(object sender, EventArgs e)
{
var app = sender as HttpApplication;
var err = app.Server.GetLastError() as HttpException;
if (err != null)
{
var customErrors = ConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection;
if (customErrors != null && customErrors.RedirectMode == CustomErrorsRedirectMode.ResponseRewrite)
{
var response = app.Context.Response;
response.StatusCode = err.GetHttpCode();
response.TrySkipIisCustomErrors = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment