-
Notifications
You must be signed in to change notification settings - Fork 0
/
Global.asax.cs
58 lines (47 loc) · 1.85 KB
/
Global.asax.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace Assignment8
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// New...
AutoMapperConfig.RegisterMappings();
}
protected void Application_EndRequest()
{
// Handling error conditions...
// Inspiration: http://stackoverflow.com/a/9026907
var code = Context.Response.StatusCode;
// Add more conditions here as you need them
if (code == 404) { this.HandleError("NotFound"); }
// if (code >= 500) { this.HandleError("ServerError"); }
}
private void HandleError(string action)
{
// This method causes the Errors controller to handle the request
// It creates the controller, then executes the desired action method
// With some more code, it could also save the error,
// and notify the web site programmer(s)
// Clear the accumulated data out of the response object
Response.Clear();
// Create the route data configuration
var rd = new RouteData();
rd.Values["controller"] = "Errors";
rd.Values["action"] = action;
// Create then execute the controller method
IController c = new Controllers.ErrorsController();
c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
}
}
}