PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/MVC/src/SystemWebMvc/Mvc/MvcHandler.cs

#
C# | 108 lines | 88 code | 15 blank | 5 comment | 7 complexity | 59376ec70557b6adfd14f332dc6b505e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. namespace System.Web.Mvc {
  2. using System;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Globalization;
  5. using System.Reflection;
  6. using System.Web;
  7. using System.Web.Mvc.Resources;
  8. using System.Web.Routing;
  9. using System.Web.SessionState;
  10. [SuppressMessage("Microsoft.Security", "CA2112:SecuredTypesShouldNotExposeFields", Justification = "There's nothing secret about the value of this field.")]
  11. public class MvcHandler : IHttpHandler, IRequiresSessionState {
  12. private ControllerBuilder _controllerBuilder;
  13. private static string MvcVersion = GetMvcVersionString();
  14. public static readonly string MvcVersionHeaderName = "X-AspNetMvc-Version";
  15. public MvcHandler(RequestContext requestContext) {
  16. if (requestContext == null) {
  17. throw new ArgumentNullException("requestContext");
  18. }
  19. RequestContext = requestContext;
  20. }
  21. protected virtual bool IsReusable {
  22. get {
  23. return false;
  24. }
  25. }
  26. internal ControllerBuilder ControllerBuilder {
  27. get {
  28. if (_controllerBuilder == null) {
  29. _controllerBuilder = ControllerBuilder.Current;
  30. }
  31. return _controllerBuilder;
  32. }
  33. set {
  34. _controllerBuilder = value;
  35. }
  36. }
  37. public static bool DisableMvcResponseHeader {
  38. get;
  39. set;
  40. }
  41. public RequestContext RequestContext {
  42. get;
  43. private set;
  44. }
  45. protected internal virtual void AddVersionHeader(HttpContextBase httpContext) {
  46. if (!DisableMvcResponseHeader) {
  47. httpContext.Response.AppendHeader(MvcVersionHeaderName, MvcVersion);
  48. }
  49. }
  50. private static string GetMvcVersionString() {
  51. // DevDiv 216459:
  52. // This code originally used Assembly.GetName(), but that requires FileIOPermission, which isn't granted in
  53. // medium trust. However, Assembly.FullName *is* accessible in medium trust.
  54. return new AssemblyName(typeof(MvcHandler).Assembly.FullName).Version.ToString(2);
  55. }
  56. protected virtual void ProcessRequest(HttpContext httpContext) {
  57. HttpContextBase iHttpContext = new HttpContextWrapper(httpContext);
  58. ProcessRequest(iHttpContext);
  59. }
  60. protected internal virtual void ProcessRequest(HttpContextBase httpContext) {
  61. AddVersionHeader(httpContext);
  62. // Get the controller type
  63. string controllerName = RequestContext.RouteData.GetRequiredString("controller");
  64. // Instantiate the controller and call Execute
  65. IControllerFactory factory = ControllerBuilder.GetControllerFactory();
  66. IController controller = factory.CreateController(RequestContext, controllerName);
  67. if (controller == null) {
  68. throw new InvalidOperationException(
  69. String.Format(
  70. CultureInfo.CurrentUICulture,
  71. MvcResources.ControllerBuilder_FactoryReturnedNull,
  72. factory.GetType(),
  73. controllerName));
  74. }
  75. try {
  76. controller.Execute(RequestContext);
  77. }
  78. finally {
  79. factory.ReleaseController(controller);
  80. }
  81. }
  82. #region IHttpHandler Members
  83. bool IHttpHandler.IsReusable {
  84. get {
  85. return IsReusable;
  86. }
  87. }
  88. void IHttpHandler.ProcessRequest(HttpContext httpContext) {
  89. ProcessRequest(httpContext);
  90. }
  91. #endregion
  92. }
  93. }