Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know this is well after the question has been answered, but I'm using a different approach as I am using dependency injection to instantiate my controllers.</p> <p>I have replaced the IActionInvoker ( by injecting the controller's ControllerActionInvoker Property ) with a version that overrides the InvokeActionMethod method.</p> <p>This means no change to controller inheritance and it can be easily removed when I upgrade to MVC4 by altering the DI container's registration for ALL controllers</p> <pre><code>public class JsonNetActionInvoker : ControllerActionInvoker { protected override ActionResult InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary&lt;string, object&gt; parameters) { ActionResult invokeActionMethod = base.InvokeActionMethod(controllerContext, actionDescriptor, parameters); if ( invokeActionMethod.GetType() == typeof(JsonResult) ) { return new JsonNetResult(invokeActionMethod as JsonResult); } return invokeActionMethod; } private class JsonNetResult : JsonResult { public JsonNetResult() { this.ContentType = "application/json"; } public JsonNetResult( JsonResult existing ) { this.ContentEncoding = existing.ContentEncoding; this.ContentType = !string.IsNullOrWhiteSpace(existing.ContentType) ? existing.ContentType : "application/json"; this.Data = existing.Data; this.JsonRequestBehavior = existing.JsonRequestBehavior; } public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } if ((this.JsonRequestBehavior == JsonRequestBehavior.DenyGet) &amp;&amp; string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) { base.ExecuteResult(context); // Delegate back to allow the default exception to be thrown } HttpResponseBase response = context.HttpContext.Response; response.ContentType = this.ContentType; if (this.ContentEncoding != null) { response.ContentEncoding = this.ContentEncoding; } if (this.Data != null) { // Replace with your favourite serializer. new Newtonsoft.Json.JsonSerializer().Serialize( response.Output, this.Data ); } } } } </code></pre> <p>--- EDIT - Updated to show container registration for controllers. I'm using Unity here.</p> <pre><code>private void RegisterAllControllers(List&lt;Type&gt; exportedTypes) { this.rootContainer.RegisterType&lt;IActionInvoker, JsonNetActionInvoker&gt;(); Func&lt;Type, bool&gt; isIController = typeof(IController).IsAssignableFrom; Func&lt;Type, bool&gt; isIHttpController = typeof(IHttpController).IsAssignableFrom; foreach (Type controllerType in exportedTypes.Where(isIController)) { this.rootContainer.RegisterType( typeof(IController), controllerType, controllerType.Name.Replace("Controller", string.Empty), new InjectionProperty("ActionInvoker") ); } foreach (Type controllerType in exportedTypes.Where(isIHttpController)) { this.rootContainer.RegisterType(typeof(IHttpController), controllerType, controllerType.Name); } } public class UnityControllerFactory : System.Web.Mvc.IControllerFactory, System.Web.Http.Dispatcher.IHttpControllerActivator { readonly IUnityContainer container; public UnityControllerFactory(IUnityContainer container) { this.container = container; } IController System.Web.Mvc.IControllerFactory.CreateController(System.Web.Routing.RequestContext requestContext, string controllerName) { return this.container.Resolve&lt;IController&gt;(controllerName); } SessionStateBehavior System.Web.Mvc.IControllerFactory.GetControllerSessionBehavior(RequestContext requestContext, string controllerName) { return SessionStateBehavior.Required; } void System.Web.Mvc.IControllerFactory.ReleaseController(IController controller) { } IHttpController IHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) { return this.container.Resolve&lt;IHttpController&gt;(controllerType.Name); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload