PageRenderTime 25ms CodeModel.GetById 42ms RepoModel.GetById 1ms app.codeStats 0ms

/Admin/Manager.WebApp/Global.asax.cs

https://gitlab.com/thanhhung95/managecourse
C# | 193 lines | 126 code | 40 blank | 27 comment | 18 complexity | 8578e90675c6c12b3d6053649a8945eb MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Http;
  6. using System.Web.Mvc;
  7. using System.Web.Optimization;
  8. using System.Web.Routing;
  9. using Manager.SharedLibs.Logging;
  10. using Manager.WebApp.Controllers;
  11. using Newtonsoft.Json;
  12. namespace Manager.WebApp
  13. {
  14. public class MvcApplication : System.Web.HttpApplication
  15. {
  16. private readonly ILog logger = LogProvider.GetCurrentClassLogger();
  17. protected void Application_Start()
  18. {
  19. AreaRegistration.RegisterAllAreas();
  20. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  21. GlobalConfiguration.Configure(WebApiConfig.Register);
  22. RouteConfig.RegisterRoutes(RouteTable.Routes);
  23. BundleConfig.RegisterBundles(BundleTable.Bundles);
  24. //This value provider will handle requests that are encoded as application/json.
  25. //There’s no need to specify a model binder on classes that accept JSON input.
  26. //ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
  27. System.Web.Helpers.AntiForgeryConfig.SuppressXFrameOptionsHeader = true;
  28. }
  29. protected void Application_Error(object sender, EventArgs e)
  30. {
  31. var httpContext = ((MvcApplication)sender).Context;
  32. var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));
  33. var currentController = " ";
  34. var currentAction = " ";
  35. if (currentRouteData != null)
  36. {
  37. if (currentRouteData.Values["controller"] != null && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString()))
  38. {
  39. currentController = currentRouteData.Values["controller"].ToString();
  40. }
  41. if (currentRouteData.Values["action"] != null && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString()))
  42. {
  43. currentAction = currentRouteData.Values["action"].ToString();
  44. }
  45. }
  46. var ex = Server.GetLastError();
  47. var currentAreaRoute = HttpContext.Current.Request.RequestContext.RouteData.DataTokens["area"];
  48. var currentArea = string.Empty;
  49. if (currentAreaRoute != null)
  50. currentArea = currentAreaRoute.ToString();
  51. if (currentArea.Contains("admin") || currentArea.Contains("Admin"))
  52. {
  53. var controller = new ErrorController();
  54. var routeData = new RouteData();
  55. var action = "Index";
  56. if (ex is HttpException)
  57. {
  58. var httpEx = ex as HttpException;
  59. switch (httpEx.GetHttpCode())
  60. {
  61. case 404:
  62. action = "NotFound";
  63. break;
  64. // others if any
  65. default:
  66. action = "Index";
  67. break;
  68. }
  69. }
  70. logger.ErrorException("Application Error: {0}", ex, ex.Message);
  71. httpContext.ClearError();
  72. httpContext.Response.Clear();
  73. httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500;
  74. httpContext.Response.TrySkipIisCustomErrors = true;
  75. //Process error for Ajax Request
  76. //http://stackoverflow.com/questions/7551424/how-to-know-if-the-request-is-ajax-in-asp-net-in-application-error
  77. bool isAjaxCall = string.Equals("XMLHttpRequest", Context.Request.Headers["x-requested-with"], StringComparison.OrdinalIgnoreCase);
  78. Context.ClearError();
  79. if (isAjaxCall)
  80. {
  81. var jsonObject = new { error = true, message = ex.Message };
  82. var jsonStringResponse = JsonConvert.SerializeObject(jsonObject);
  83. Context.Response.ContentType = "application/json";
  84. Context.Response.Write(jsonStringResponse);
  85. return;
  86. }
  87. routeData.Values["controller"] = "../Areas/Admin/Views/Error";
  88. routeData.Values["action"] = action;
  89. //Response.TrySkipIisCustomErrors = true; // If you are using IIS7, have this line
  90. //IController errorsController = new ErrorController();
  91. //HttpContextWrapper wrapper = new HttpContextWrapper(Context);
  92. //var rc = new System.Web.Routing.RequestContext(wrapper, routeData);
  93. //errorsController.Execute(rc);
  94. //IController controller = new ErrorController();
  95. //controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
  96. //Response.End();
  97. controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction);
  98. ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
  99. Response.End();
  100. }
  101. else
  102. {
  103. var controller = new ErrorController();
  104. var routeData = new RouteData();
  105. var action = "Index";
  106. if (ex is HttpException)
  107. {
  108. var httpEx = ex as HttpException;
  109. switch (httpEx.GetHttpCode())
  110. {
  111. case 404:
  112. action = "NotFound";
  113. break;
  114. // others if any
  115. default:
  116. action = "Index";
  117. break;
  118. }
  119. }
  120. logger.ErrorException("Application Error: {0}", ex, ex.Message);
  121. httpContext.ClearError();
  122. httpContext.Response.Clear();
  123. //httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500;
  124. //httpContext.Response.TrySkipIisCustomErrors = true;
  125. //Process error for Ajax Request
  126. //http://stackoverflow.com/questions/7551424/how-to-know-if-the-request-is-ajax-in-asp-net-in-application-error
  127. bool isAjaxCall = string.Equals("XMLHttpRequest", Context.Request.Headers["x-requested-with"], StringComparison.OrdinalIgnoreCase);
  128. Context.ClearError();
  129. if (isAjaxCall)
  130. {
  131. var jsonObject = new { error = true, message = ex.Message };
  132. var jsonStringResponse = JsonConvert.SerializeObject(jsonObject);
  133. Context.Response.ContentType = "application/json";
  134. Context.Response.Write(jsonStringResponse);
  135. return;
  136. }
  137. routeData.Values["controller"] = "Error";
  138. routeData.Values["action"] = action;
  139. //Response.TrySkipIisCustomErrors = true; // If you are using IIS7, have this line
  140. //IController errorsController = new ErrorController();
  141. //HttpContextWrapper wrapper = new HttpContextWrapper(Context);
  142. //var rc = new System.Web.Routing.RequestContext(wrapper, routeData);
  143. //errorsController.Execute(rc);
  144. //IController controller = new ErrorController();
  145. //controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
  146. //Response.End();
  147. controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction);
  148. ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
  149. Response.End();
  150. }
  151. }
  152. }
  153. }