Framework Madness!

And other adventures C# and … MVC – HandleUnknownAction in Controller

with one comment

Sorry I have been away so long. Work has been very busy and I have been doing a lot of planning for new projects, reading, rebuilding the notebook and trying to endure winter. Needless to say I have been away for a while.

So … I have started to look in building MetaWeblog support into an MVC project and have found a dearth of examples. So after doing some reading, testing, reflectoring, etc. I have decided to role my own. “Why not?” I figure.

The first thing I tried was to create a basic MetaWeblog action model and custom model binder. The model binder (which was easy to make) worked, but it didn’t solve the big problem – I want to create individual methods for each MetaWeblog RPC, and that wouldn’t cut it. I considered making a filter, but that wouldn’t work either. So the the next thing was to try to hack the controller, and that seems to work nicely.

One of the things the MVC team has done a nice job is keeping the architecture of the MVC platform simple and open. I started using reflector to find an easy access point to inject a change to action method called and sure enough I found it the ‘HandleUnknownAction’ of the Controller class. I’ll show an example and then explain:

Code Snippet
  1. public class UnknownController : Controller
  2. {
  4.     protected override void HandleUnknownAction(string actionName)
  5.     {
  6.         if (!String.IsNullOrEmpty(actionName))
  7.         {
  8.             //set unknown routevalue
  9.             ControllerContext.RouteData.Values["UnknownValue"] = DateTime.Now.ToString();
  11.             if (this.ActionInvoker.InvokeAction(this.ControllerContext, "UnknownAction"))
  12.             {
  16.                 return;
  17.             }
  18.         }
  20.         base.HandleUnknownAction(actionName);
  21.     }
  24.     public ActionResult UnknownAction([DefaultValue("")]String UnknownValue)
  25.     {
  26.         return Content(String.Format("UnknownAction: {0} : {1}", RouteData.Values["action"],UnknownValue));
  27.     }
  30. }

The highlights:

  • So for a simple test I just wanted to redirect the action of the ‘actionName’ parameter existed. And unless you change the route defaults it’s ‘index’ – so we are good here. (The base method of HandleUnkownAction is simple – it just throws an Http error. )
  • Next I injected an new route value keyed as ‘UnknownValue’ and this matches a parameter on the ‘UnknownAction’ method I am going to call. (Yes! It does bind!’)
  • Last we call the ‘InvokeAction’ method on the ActionInvoker property of the controller. Important: you must return if the of the InvokeAction method succeeds. And the rest just works as if your calling a normal action method.
  • The results are simple. Here is an example:
    UnknownAction: Index : 1/14/2010 12:54:53 AM

So I am thinking that this would be a good basis for creating a MetaWeblog API handler using basic MVC conventions. I’ll provide more information as I get further along.


Written by Lynn Eriksen

January 14, 2010 at 1:05 am

Posted in Uncategorized

Tagged with ,

One Response

Subscribe to comments with RSS.

  1. […] to MVC – HandleUnknownAction in Controller (1/13/2010)Wednesday, January 13, 2010 from leriksen71.wordpress.comOne of the things the MVC team has done a […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: