There’s a class ControllerActionInvoker in ASP.NET MVC. This can be used as one of an hook-points to allow customization of your application. Watching Brad Wilsons’ Advanced MP3 from MVC Conf inspired me to write about this class. What MSDN says:“Represents a class that is responsible for invoking the action methods of a controller.”Well if MSDN says it, I think I can instill a fair amount of confidence into what the class does. But just to get to the details, I also looked into the source code for MVC. Seems like the base class Controller is where an IActionInvoker is initialized:protected virtual IActionInvoker CreateActionInvoker() {
http://site.com/RenderAs/xml
http://site.com/RenderAs/csv … and so on where RenderAs is my controller.Read more: IBloggable - implemented
return new ControllerActionInvoker();}In the ControllerActionInvoker (the O-O-B behavior), there are different ‘versions’ of InvokeActionMethod() method that actually call the action method in question and return an instance of type ActionResult. protected virtual ActionResult InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary<string, object> parameters) {
object returnValue = actionDescriptor.Execute(controllerContext, parameters);}I guess that’s enough on the ‘behind-the-screens’ of this class. Let’s see how we can use this class to hook-up extensions. Say I have a requirement that the user should be able to get different renderings of the same output, like html, xml, json, csv and so on. The user will type-in the output format in the url and should the get result accordingly. For example: http://site.com/RenderAs/ – renders the default way (the razor view)
ActionResult result = CreateActionResult(controllerContext, actionDescriptor, returnValue);
return result;
http://site.com/RenderAs/xml
http://site.com/RenderAs/csv … and so on where RenderAs is my controller.Read more: IBloggable - implemented
0 comments:
Post a Comment