Created
July 30, 2014 02:49
-
-
Save imwower/777f3466ce6ebef1c5ad to your computer and use it in GitHub Desktop.
ASP MVC Application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MvcApplication : HttpApplication | |
{ | |
public MvcApplication() | |
{ } | |
void Application_Error(object sender, EventArgs e) | |
{ | |
var httpContext = ((MvcApplication)sender).Context; | |
var exception = Server.GetLastError(); | |
Server.ClearError(); | |
httpContext.ClearError(); | |
httpContext.Response.Clear(); | |
httpContext.Response.StatusCode = exception is HttpException ? ((HttpException)exception).GetHttpCode() : 500; | |
httpContext.Response.TrySkipIisCustomErrors = true; | |
var handlers = DependencyResolver.Current.GetServices<HandleErrorAttribute>(); | |
var handler = handlers.FirstOrDefault(); | |
if (handler != null) | |
{ | |
var filterContext = new ExceptionContext | |
{ | |
ExceptionHandled = false, | |
Exception = exception | |
}; | |
handler.OnException(filterContext); | |
} | |
} | |
protected void Application_Start() | |
{ | |
StartFramework(); | |
AreaRegistration.RegisterAllAreas(); | |
foreach (var filter in DependencyResolver.Current.GetServices<HandleErrorAttribute>()) | |
GlobalFilters.Filters.Add(filter); | |
RouteConfig.RegisterRoutes(RouteTable.Routes); | |
} | |
private void StartFramework() | |
{ | |
var bootstrapper = new Bootstraper(); | |
var builder = bootstrapper.Init(); | |
builder.RegisterInstance<HttpApplication>(this).SingleInstance(); | |
bootstrapper.Start(); | |
var container = bootstrapper.Container; | |
DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment