Archive for December, 2007

MS MVC Gets Blocks From Rails

Updated 2008-01-16:

Check out the refactored version which drops the reflection and uses a straight response filter.


Sergio Pereira and I started writing some stories today for Javascript helpers in Mvc Contrib.  During our talk we had to deal with rendering html elements with inner html and javascript blocks ensuring they all got closed but still allowing the ultimate in flexibility for the developer.MvcToolkit goes about this with a using(…) pattern on their FormHelper.  The one disadvantage of this is that the using block will be limited to only a single block.  What happens when you need to render a block for onSuccess and onFailure?

Sergio brought up how powerful ruby blocks are and how cool it would be if we could use lambdas to do something similar.

Sergio proposed being able to do something like this.

<% Ajax.Tag(”div“,  “/controller/action/123“,
new {CssClass=”bigSquare“}, myDiv =>
{ %>
Some Html here
Some helper: <%= Html.Link(”Click me“, “/other/non-ajax/url“) %>
}) %>

What is myDiv?  myDiv is the blocks outer HtmlElement object so inside of your lambda you can manipulate its attributes and perform some other cool stuff we are still cooking up.  While the lambda executes we capture the output and defer rendering to the response until after the lambda is finished.  This allows us to render the HtmlElement with any modifications you made.

So how do we do this, well, we needed a pretty big hack because our delegate is being called from another anonymous delegate which has the HtmlTextWriter as a local variable.  This means that we cannot use IHttpContext.SwitchWriter to capture our output.

Any way to get a fix for this MS?

So in reflector I noticed my anonymous type for my delegate had a public field __w which was the HtmlTextWriter.  Using some reflection I was able to code up my own SwitchWriter which took care of everything.

The helper code is pretty simple.

public static void FromTag(this AjaxHelper helper
  ,string tag
  ,string url
  ,object options,
  Action<Element> innerHtml)
{
  Element element = new Element(tag);
  HtmlTextWriter responseWriter = null;

  if(innerHtml != null)
  {
      try
      {
        using(StringWriter innerWriter = new StringWriter())
        using (HtmlTextWriter innerHtmlWriter =
          new HtmlTextWriter(innerWriter))
        {
          responseWriter = SwitchWriter(innerHtml, innerHtmlWriter);
          innerHtml(element);
          innerHtmlWriter.Flush();
          element.InnerHtml = innerWriter.ToString();
        }
      }
      finally
      {
        if(responseWriter != null)
        {
          SwitchWriter(innerHtml, responseWriter);
        }
      }
  }
  RenderElement(new HtmlTextWriter(
    helper.ViewContext.HttpContext.Response.Output), element);
}

The switch writer is pretty basic, it was tracking it down that took a little bit of time.

public static HtmlTextWriter SwitchWriter(object obj
  , HtmlTextWriter newWriter)
{
  Type actionType = obj.GetType();

  object target = actionType.GetField(”_target“,
    BindingFlags.NonPublic | BindingFlags.Instance)
    .GetValue(obj);

  Type targetType = target.GetType();

  HtmlTextWriter response = targetType.GetField(”__w“)
    .GetValue(target) as HtmlTextWriter;

  targetType.GetField(”__w“).SetValue(target, newWriter);

  return response;
}

With the above in place I can call it with something like this.

<% Ajax.FromTag("div“, “/Home/About“,
  new object(),
  myDiv => { myDiv.Id = “Justice“; %><h6>
      <%=Html.ActionLink(”Where is Justice?“, “About“)%>
   </h6>
<% }); %>

And it renders something like this… no we didn’t spike any ajax stuff yet.

<div id=“Justice”>
 <h6><a href=“/Home/About”>Where is Justice?</a></h6>
</div>

You may wonder why we do not return a string from our helper.  Asp.Net currently can’t handle that.  It thinks its a block and not a simple expression so using the regular <%= %> will not work.  Instead of having you call <%Response.Write(…);%> we thought it would be easier if the helper just rendered it for you.

This got me excited about some the cool things we can really start to do.  I like getting an instance of Element to set properties on, this can help reduce the abuse that anonymous types are taking in mvc right now.  It would be great if there could be patch so we do not have to resort to reflection to switch writers, but I quote Sergio

how do you feel about that __w hack? For me it looks prettier by the minute

Let me know what you think,

Cheers

Browser Wars are Heating Up

Looks like IE8 can render the acid2 face test. Nice work IE team, maybe we will start seeing more releases out of Microsoft with some higher quality. Today, Jeff talks about the javascript showdown between browsers. I agree with him, competition is a good thing.

Now if I could only get a FF 2.0 and FF 3.0 side by side install going.

Tags: ,

Haml Comes to MS MVC

Andrew Peter has created NHaml, a new view engine for MS MVC based on the ruby Haml dsl. Sweet!! Maybe we can get Andrew to drop this in the MVC Contrib project.

The community support for MS MVC has been awesome so far.

Tags: ,

MS MVC: Routing - The Good, The Bad, The RESTful

After playing with MS MVC for a little bit, and looking at routing extensively I have compiled a list of good things and things that need some improvement. The team has already said they will be doing work with routing, I am looking forward to seeing what they come up with.

What’s Good with MS MVC Routing?

Not Bound Explicitly To Controllers and Actions

An innovative approach to routing was not forcing the controller and action to be bound to the route. Instead they introduced two special parameters [controller] and [action] to indirectly bind the request. This drastically reduces the number of routes your application needs and means you do not have to register a route for every new controller.

Validation

Validation separates the url from matching on the parameters. It allows for full blown regular expressions without dirtying up the url and making it more difficult to generate urls from the routes. Hammett ran into this issue with his new routing module for monorail when he offered a straight regex rule. There was no way to pull parameters out of it.

Route Handlers

A very simple way to extend route resolution on incoming requests. Its not complicated and it gives you access to everything you need to do custom matching. I wish we could get something like that for generating urls.

Restful

Not much more to say hear that hasn’t already been said. This engine does make writing restful urls a breeze.

Testing

Thank you for making my routes so easy to test. I now have confidence that everything is working without having to fire up IIS and a browser to test by hand.

What’s Not So Good with MS MVC Routing?

No Duplicate Controller Names?

I can’t have two controllers in different namespaces with the same name?

Solution

Phil said they are fixing this. I would hope I could specify a Controller parameter either as a Type or a String where the string could be the full name of the controller. I would also maybe like this to be addressed with Areas… maybe.

Areas Where Are You?

There is no out of the box support for Areas. An area is simply a partition for your application. The most common one I can think of is http://localhost/admin/[controller] for separating all of your admin stuff from regular url’s.

Another scenario would be dropping in secondary applications, like a forum, blog, photo gallery, etc. Those will probably need to be defined under some Area.

Workarounds

RouteTable.Routes.Add(new Route
{
 Url = “admin/[controller]/[action]“,
 Defaults = new
 {
  Controller = “Admin“,
  Action = “Index”
 },
 Validation = new
 {
  Controller = “Admin|Users|Categories”
 },
 RouteHandler = typeof(MvcRouteHandler)
});

What we did above is hard code are “admin” area and then set a Validation regular expression on Controller. Now only the controllers listed will be accessible via those urls, and when generating a url it will have the correct prefix of “admin/” in the path.

What sucks about that? Every time I add a new controller that should be an admin controller I have to modify the regular expression. BTW, that regex is not case insensitive like the url.

Potential Solutions

  1. Allow a convention where the namespace dictates the area.
  2. Add an AreaAttribute similar to Monorail.

I like having the option of number 1, but I don’t want to be forced to this. It makes my urls feel to tightly coupled to my code. Its not a bad default convention as I am sure it will work the vast majority of the time.

Number 2 just doesn’t sit right with me but it seems like a decent option as well. The area has to come from somewhere, right?

One thing is for sure, there needs to be some type of Area parameter that is respected by the RouteCollection and based on the current implementation it looks like it would be very difficult for it to support areas deeper than 1 level. By default it is splitting on the “/” so an area covering “admin/maintenance” would be difficult to use with a single Area parameter.

Maybe it could support arrays which could get interesting…

[area]/[area]/[area]/[controller]/[action]
Defaults = new { Area = "admin/maintenance/daily" }
Validation = new { Area = "admin/maintenance/daily" }

Are We Really Blocking and Locking?

Reflecting over the code of the RouteCollection we can see that when an operation is performed like GetRouteData or GetUrl then the routes are locked until the proper route is matched. Every request coming into your application is going to call GetRouteData and every link you render in your views is going to call GetUrl. What does this mean. It means when a request comes in it locks the routes searching for a match. When a new request comes in on a separate thread it calls GetRouteData too and sits and waits until the first request found a match before it can proceed. And all the blocking happens for every link that you are rendering in your views too. Most pages will have 10 - 20 links, that’s lot of blocking, idle time, and context switches for the OS.

Solution

Don’t lock, period. Adding routes should go into a temp route collection. Add explicit methods for Build() or Rebuild() which can perform an atomic assignment operation on the temp routes to the routes in use. Can some kind of error happen if routes are switch in the middle of processing a request? Yes. What are the odds? Slim. If dynamically altering routes is something that my application requires, without restarting it, then let me handle that. Its an edge case.

If you have to lock, think about using a lock free collection, or use Reader/Writer locks as the number of reads is going to demolish the number of writes. Locking is just way too expensive to do on every request and every link render.

Why is Validation Case Sensitive?

Urls should be case insensitive. If I want to validate parameters using a regular expression those should be case insensitive to. Please don’t make me write Validation expression like: @”[eE][dD][iI][tT]” to match any combination of the string “Edit”.

Why is Validation Not Compiled?

Every request coming in is going to call GetRouteData, does it not make sense to compile my Validation expressions so they do not need to parsed every time they are evaluated?

Solution

if (!Regex.IsMatch(input, pattern))
{
 return false;
}

to

foreach(Regex validator in route.Validators)
{
 //…
 if(!validator.IsMatch(input))
 {
  return false;
 }
 //…
}

Where is the Extensibility?

No, not this extensibility, well maybe, give me the option please.

There are a number of edge cases popping up on blogs, comments, and forum posts.

  • What do I do if I need a different sub/domain?
  • What do I do if I need to redirect to a secure url from an insecure one?
  • What do I do if I need an absolute url instead of a relative one?
  • What do I do if I need to redirect to a non-standard port for https?

While the MVCRouteHandler is extendable the Route is not, nor is the RouteCollection. We need a way to override the matching AND url generating for specific rules.

Potential Solution

The RouteCollection looks a little heavy. I think it can delegate some of its responsibility to other services whose implementations can be swapped out. I also think it would be prudent to give us some interfaces and factories. Just overriding the GetRouteData and GetUrl is not enough. It would be great to have the ability to create our own RouteCollection so we could implement different algorithms for collecting and enumerating the routes to find a match?

Summary

All in all I am very pleased with routing in the first CTP. IMHO it is better than Monorail routing right now. It does need some pieces re-thought, re-factored, and de-debugged, but if it were perfect it wouldn’t be CTP and I wouldn’t be writing this.

Please let me know what I am missing and how crazy some of my solutions are.

+1 To the MS-MVC team, they are off to a great start.

Tags: , ,

Vim Screencast Teaser

Aaron just posted a kick ass teaser about using vim. I want the whole series on dvd now.

Tags: ,

Toggle Between Tabs & Spaces With Ease

Ever get yelled at for using tabs instead of spaces or spaces instead of tabs?  Here’s your cure it toggles the text editor settings in visual studio for you.

Public Sub ToggleTabsAndSpaces()
 Dim keys As String() = { _
  “Basic“, _
  “C/C++“, _
  “CSharp“, _
  “CSS“, _
  “HTML“, _
  “PL/SQL“, _
  “PlainText“, _
  “T-SQL“, _
  “XML“}

 Dim turnTabsOn As Boolean = True
 Dim msg = “Truning Tabs OnIf DTE.Properties(”TextEditor“, keys(0)) _
   .Item(”InsertTabs“).Value = True Then
   turnTabsOn = False
   msg = “Turning Tabs OffEnd If

 IdeHelper.StatusBar(msg)
 IdeHelper.Output(msg)

 With DTE.Properties(”TextEditor“, “AllLanguages“)
   .Item(”TabSize“).Value = 2
   .Item(”InsertTabs“).Value = 2
 End With

 For Each key As String In keys
   DTE.Properties(”TextEditor“, key) _
   .Item(”InsertTabs“).Value = turnTabsOn
 Next
End Sub

I mapped mine to Ctrl+Shift+=

If you need to find out what the names of the different editors are you can look them up in the registry.

x64
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\9.0\AutomationProperties\TextEditor
x32
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\AutomationProperties\TextEditor
Tags: ,

Creating Patches for Subversion from Git

What a dreadful way to spend the last hour I wish it upon no one else. I recently started using Git and git-svn to track some of my projects and I almost completely abandoned it because creating an svn friendly patch that something like TortoiseMerge could apply is impossible. Then I found the git-svn-utils project. It doesn’t look like its active but it does have a script git-svn-diff which is exactly what I was looking for. Unfortunately the script was not handling new files the way subversion, or maybe TortoiseMerge wanted. So I made some pretty bad hacks and came up with this version. It will create a full patch file that can be applied with TSVN.

I am interested to know if I am the only one out there that had this problem or if there is a better solution. Please let me know as I am a total git newb.

Download git-svn-diff

Tags: ,

MS MVC: Testing Routes

In Scott’s post about routing he showed how you can easily test routes. One cool thing about 3.5 asp.net extension framework is the introduction of new interfaces for IHttpContext, IHttpRequest, IHttpResponse, and some other. This makes testing with mocks so much easier. Here I will show you some tests I wrote to validate the routing rules we created for simply restful routing in the ms mvc framework.

What Are We Testing?

When testing we want to stick with one assertion per test. So for this example we are only testing that the rules we wrote set the proper action. You can view the rules we will be testing in my previous post.

Create The Test Fixture

using System.Collections.Specialized;
using System.Web;
using System.Web.Mvc;
using MVCContrib.SimplyRestful;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Mocks;

namespace MVCContrib.Specs.SimplyRestfulSpecs
{
  [TestFixture]
  public class SimplyRestfulRouteMatchTests
  {
  }
}

Add The First Test

    [Test]
    public void GetRouteData_WithAControllerAndIdUsingAHttpGetRequest_SetsTheShowAction()
    {
    }

Our test name is using pattern Method_Context_Result. So the method under test is GetRouteData. The context of the test is With a controller and id using a an http get request sets the show action. Now lets fill in the test.

    [Test]
    public void GetRouteData_WithAControllerAndIdUsingAHttpGetRequest_SetsTheShowAction()
    {
      IHttpContext httpContext;
      RouteCollection routeCollection = new RouteCollection();
      SimplyRestfulRouteHandler.InitializeRoutes(routeCollection);
      RouteData routeData = routeCollection.GetRouteData(httpContext);
      Assert.That(routeData.Values[”action“], Is.EqualTo(”show“).IgnoreCase);
    }

Above we wrote our assertion. We want to call the GetRouteData method on a routeCollection and we want to assert that the parameter named “action” is equal to show. At this point the test will fail with a nre on httpContext. httpContext needs to be an instance of IHttpContext, so how do we get an instance of an interface. Simple, we mock it. We will setup the mock context with just enough information for the RouteCollection to do its job. This could be considered a stub and not a mock since we are simply using Rhino.Mocks to setup results.

    [Test]
    public void GetRouteData_WithAControllerAndIdUsingAHttpGetRequest_SetsTheShowAction()
    {
      MockRepository mocks = new MockRepository();
      IHttpContext httpContext = mocks.DynamicMock<IHttpContext>();
      IHttpRequest httpRequest = mocks.DynamicMock<IHttpRequest>();

      RouteCollection routeCollection = new RouteCollection();
      SimplyRestfulRouteHandler.InitializeRoutes(routeCollection);

      using(mocks.Record())
      {
        SetupResult.For(httpContext.Request)
          .Return(httpRequest);
        SetupResult.For(httpRequest.AppRelativeCurrentExecutionFilePath)
          .Return(”~/products/123“);
        SetupResult.For(httpRequest.HttpMethod)
          .Return(”GET“);
      }
      using(mocks.Playback())
      {
        RouteData routeData = routeCollection.GetRouteData(httpContext);
        Assert.That(routeData.Values[”action“], Is.EqualTo(”show“).IgnoreCase);
      }
    }

So what just happened? First we created a MockRepository which is the core engine of Rhino.Mocks. This could probably get moved to a [SetUp] method. Next we create two Dynamic Mocks. One for the context and one for the request. We make them dynamic mocks because they are not under test. We do not care what gets called on these interfaces or how it gets called. We just care that if certain properties are called they return a specific values emulating what a real http request would look like. Next we have the mocks.Record phase. This is used to demarcate expectations versus assertions.

What to note is that the RouteCollection uses the AppRelativeCurrentExecutionFilePath property which expects a return string like “~/something/something/something.aspx” we are telling it to return “~/products/123″ products is our controller and 123 should be our id of the route. Next we setup a result for the Httpmethod property, remember our Route had validation on the special name Method which maps to a call on HttpMethod.

Finally we run our test and make the assertions. We enter playback mode, execute the GetRouteData method, which is our method under test, pass in the mocked context and assert that the returned routedata has a parameter named “action” = “show”

So once you add a couple more tests for different routes you will see a way to factor some common code out. I created a helper method to setup my context.

    private void SetupContext(string url, string httpMethod, string formMethod)
    {
      SetupResult.For(httpContext.Request).Return(httpRequest);
      SetupResult.For(httpRequest.AppRelativeCurrentExecutionFilePath).Return(url);
      SetupResult.For(httpRequest.HttpMethod).Return(httpMethod);
      if(!string.IsNullOrEmpty(formMethod))
      {
        NameValueCollection form = new NameValueCollection();
        form.Add(”_method“, formMethod);
        SetupResult.For(httpRequest.Form).Return(form);
      }
    }

Then my tests simply look like this.

    [Test]
    public void GetRouteData_WithAControllerAndIdUsingFormMethodPut_UsesSimplyRestfulRouteHandler()
    {
      using (mocks.Record())
      {
        SetupContext(”~/controller/123“, “POST“, “PUT“);
      }
      using (mocks.Playback())
      {
        RouteData routeData = routeCollection.GetRouteData(httpContext);
        Assert.That(routeData.Route.RouteHandler, Is.EqualTo(typeof(SimplyRestfulRouteHandler)));
      }
    }

The best part about writing tests for this stuff is you no longer have to fire up a browser and test by hand. You get instant verification that runs quick.

If you are wondering how I figured out what to mock and setup results for it was a quick perusal of System.Web.Extensions in reflector. Having to open reflector to figure out what to mock is a code smell for me. You really shouldn’t be mocking things you don’t own or control but this is a nice quick integration test that can save you tons of time down the road. Just don’t be surprised if after an update you get some errors because MS changed the implementation of RouteCollection.GetRouteData, another great reason for using a ContextSetup method.

Tags: , ,

MS MVC: Simply Restful Routing

MS MVC first public code drop is available. I hope you have read through all of ScottGu’s posts to help prep you for the drop. If you haven’t I highly suggest reading about Routing first before you dive into this.

What we are going to do is build a Simply Restful Route Handler similar to what is available in rails. MS MVC supports restful routing pretty good out of the box. The only case it doesn’t pick up is when browsers cannot send real HTTP PUT and DELETE requests. To get around this we are going to build a very simple route handler to inspect a hidden field on the submitted form to set the intended action of the request.

We will be creating 8 restful routes, contrary to the 7 routes in simply restful.

The Eight Actions

  • Show : handles a GET request for a displaying a single resource that the controller is representing.
  • Create : handles a POST request for a creating a new resource.
  • Update : handles a PUT request for updating an existing resource.
  • Destroy : handles a DELETE request on a resource.
  • Index : handles a GET request for a collection of resources.
  • New : handles a GET request for a blank form for creating a new resource
  • Edit : handles a GET request for a form with values filled in from the resource for updating.
  • Delete : handles a GET request a confirmation / form with options for deleting a resource. * This is the extra action.

The Routes

Action Url Http Method Form Method
Show [controller]/[id] GET
Create [controller] POST
Update [controller]/[id] PUT
Update [controller]/[id] POST PUT
Destroy [controller]/[id] DELETE
Destroy [controller]/[id] POST DELETE
Index [controller] GET
New [controller]/new GET
Edit [controller]/[id]/edit GET
Delete [controller]/[id]/delete GET

The Simply Restful Routes In MS MVC

routeCollection.Add(new Route
{
  Url = “[controller]/new“,
  Defaults = new { Action = “new” },
  RouteHandler = typeof(MvcRouteHandler)
});
routeCollection.Add(new Route
{
  Url = “[controller]/[id]/[action]“,
  Validation = new
  {
    Method = “GET“,
    Id = idValidationRegex ?? MatchAny,
    Action = “[eE][dD][iI][tT]|[dD][eE][lL][eE][tT][eE]”
  },
  RouteHandler = typeof(MvcRouteHandler)
});

routeCollection.Add(new Route
{
  Url = “[controller]/[id]“,
  Validation = new
  {
    Method = “POST“,
    Id = idValidationRegex ?? MatchAny,
  },
  RouteHandler = typeof(SimplyRestfulRouteHandler)
});

routeCollection.Add(new Route
{
  Url = “[controller]/[id]“,
  Defaults = new { Action = “show” },
  Validation = new
  {
    Method = “GET“,
    Id = idValidationRegex ?? MatchAny,
  },
  RouteHandler = typeof(MvcRouteHandler)
});

routeCollection.Add(new Route
{
  Url = “[controller]/[id]“,
  Defaults = new { Action = “update” },
  Validation = new
  {
    Method = “PUT“,
    Id = idValidationRegex ?? MatchAny
  },
  RouteHandler = typeof(MvcRouteHandler)
});

routeCollection.Add(new Route
{
  Url = “[controller]/[id]“,
  Defaults = new { Action = “destroy” },
  Validation = new
  {
    Method = “DELETE“,
    Id = idValidationRegex ?? MatchAny
  },
  RouteHandler = typeof(MvcRouteHandler)
});

routeCollection.Add(new Route
{
  Url = “[controller]“,
  Defaults = new { Action = “index” },
  Validation = new { Method = “GET” },
  RouteHandler = typeof(MvcRouteHandler)
});

routeCollection.Add(new Route
{
  Url = “[controller]“,
  Defaults = new { Action = “create” },
  Validation = new { Method = “POST” },
  RouteHandler = typeof(MvcRouteHandler)
});

I am not going to explain much of what is going on above, have a look at ScottGu’s post on routing for that. What I will point out are a couple things. First we use use some hard matches like [controller]/new and map that with a Default action. Very straightforward but just a good example of how Routes and Defaults work. Second we add some validation. Scott covered the standard validation that accepts a regex. You will notice that the regex is case sensitive so we end up with a pretty wacky looking string for “Edit|Delete”. Third, we are using a special validation property named Method which will match on the HttpMethod of the Request. And finally notice we only have a single rule that will require a custom IRouteHandler.

So lets start with a story for our new custom route handler:

Story: RouteHandler Assigns Restful Actions

As a Route Handler
I want standard post requests to route to restful actions.
So that people without javascript enabled or incompatible browsers can still have a restful experience and my web server needs to issue less browser redirects.

Acceptance Criteria:

Scenario 1: Form _method PUT Triggers Update Action
GIVEN the form has a field named _method with a value of PUT
AND the url matches [controller]/[id]
AND the HTTP method is POST
WHEN the form is posted
THEN the route action should be Update

Scenario 2: DELETE Triggers Destroy Action
GIVEN the form has a field named _method with a value of DELETE
AND the url matches [controller]/[id]
AND the HTTP method is POST
WHEN the form is posted
THEN the route action should be Delete

TDD First

To make testing easier I will be using Rhino.Mocks to create dynamic mocks of the IHttpContext and IHttpRequest. Thanks Microsoft for finally giving us interfaces to make our testing easier.

[TestFixture]
[Category("SimplyRestfulSpecs“)]
public class When_The_Form_Is_Posted_With_A_Form_Field_Named_Method_And_A_Value_Of_PUT
: BaseRouteHandlerTestFixture
{
[SetUp]
protected override void GivenSetupContext()
{
  base.GivenSetupContext();
  form.Add(”_method“, “PUT“);
}

[Test]
public void Then_The_Route_Action_Should_Be_Set_To_Update()
{
  RestfulAction action = RestfulAction.None;
  IRestfulActionResolver resolver = new RestfulActionResolver();

  using(mocks.Record())
  {
    SetupResult.For(httpContext.Request).Return(httpRequest);
    SetupResult.For(httpRequest.RequestType).Return(”POST“);
    SetupResult.For(httpRequest.Form).Return(form);
    requestContext = new RequestContext(httpContext, routeData);
  }

  using(mocks.Playback())
  {
    action = resolver.ResolveAction(requestContext);
    Assert.That(action, Is.EqualTo(RestfulAction.Update));
  }
}
}

[TestFixture]
[Category(”SimplyRestfulSpecs“)]
public class When_The_Form_Is_Posted_With_A_Form_Field_Named_Method_And_A_Value_Of_DELETE
: BaseRouteHandlerTestFixture
{
[SetUp]
protected override void GivenSetupContext()
{
  base.GivenSetupContext();
  form.Add(”_method“, “DELETE“);
}

[Test]
public void Then_The_Route_Action_Should_Be_Set_To_Destroy()
{
  RestfulAction action = RestfulAction.None;
  IRestfulActionResolver resolver = new RestfulActionResolver();

  using (mocks.Record())
  {
    SetupResult.For(httpContext.Request).Return(httpRequest);
    SetupResult.For(httpRequest.RequestType).Return(”POST“);
    SetupResult.For(httpRequest.Form).Return(form);
    requestContext = new RequestContext(httpContext, routeData);
  }

  using (mocks.Playback())
  {
    action = resolver.ResolveAction(requestContext);
    Assert.That(action, Is.EqualTo(RestfulAction.Destroy));
  }
}
}
  #region BaseRouteHandlerTestFixture
  public abstract class BaseRouteHandlerTestFixture
  {
    protected MockRepository mocks;
    protected IHttpContext httpContext;
    protected IHttpRequest httpRequest;
    protected RouteData routeData;
    protected RequestContext requestContext;
    protected NameValueCollection form;

    protected virtual void GivenSetupContext()
    {
      mocks = new MockRepository();
      httpContext = mocks.DynamicMock<IHttpContext>();
      httpRequest = mocks.DynamicMock<IHttpRequest>();

      routeData = new RouteData();
      routeData.Values.Add(”controller“, “testcontroller“);
      routeData.Values.Add(”action“, “update“);

      form = new NameValueCollection();
    }
  }
  #endregion

The Code

public enum RestfulAction
{
None = 16384,
Show = 1,
Create = 2,
Update = 4,
Destroy = 8,
Index = 16,
New = 32,
Edit = 64,
Delete = 128
}

public class SimplyRestfulRouteHandler : MvcRouteHandler
{
private const string MatchAny = “*“;

private IRestfulActionResolver actionResolver;

public SimplyRestfulRouteHandler()
{
actionResolver = new RestfulActionResolver();
}

public SimplyRestfulRouteHandler(IRestfulActionResolver actionResolver)
{
this.actionResolver = actionResolver;
}

protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
EnsureActionResolver(requestContext.HttpContext);

RestfulAction action = actionResolver.ResolveAction(requestContext);
if (action != RestfulAction.None)
{
requestContext.RouteData.Values[”action“] = action.ToString();
}
return base.GetHttpHandler(requestContext);
}

private void EnsureActionResolver(IServiceProvider serviceProvider)
{
if (actionResolver == null)
{
actionResolver = (IRestfulActionResolver)
serviceProvider.GetService(typeof(IRestfulActionResolver));
if (actionResolver == null)
actionResolver = new RestfulActionResolver();
}
}
}

public interface IRestfulActionResolver
{
RestfulAction ResolveAction(RequestContext context);
}

public class RestfulActionResolver : IRestfulActionResolver
{
public RestfulAction ResolveAction(RequestContext context)
{
if (context == null ||
context.HttpContext == null ||
context.HttpContext.Request == null)
{
throw new NullReferenceException(”Request in current HttpContext cannot be null.“);
}

if (string.IsNullOrEmpty(context.HttpContext.Request.RequestType))
{
return RestfulAction.None;
}

string requestType = context.HttpContext.Request.RequestType.ToLowerInvariant();
if (string.Equals(requestType, “post“, StringComparison.Ordinal))
{
return ResolvePostAction(context);
}

return RestfulAction.None;
}

private static RestfulAction ResolvePostAction(RequestContext context)
{
if (context.HttpContext.Request.Form == null)
{
return RestfulAction.None;
}

string formMethod = context.HttpContext.Request.Form[”_method“];
if (string.IsNullOrEmpty(formMethod))
{
return RestfulAction.None;
}

formMethod = formMethod.Trim();
if (string.Equals(”put“, formMethod, StringComparison.OrdinalIgnoreCase))
{
return RestfulAction.Update;
}
else if(string.Equals(”delete“, formMethod, StringComparison.OrdinalIgnoreCase))
{
return RestfulAction.Destroy;
}

return RestfulAction.None;
}
}

Conclusion

The only opinion I have so far on MS-MVC is it beats the heck out of WebForms and I am glad to see MS is committed to supporting a product that fits this space. In the coming weeks I should have much more about the framework. I just wanted to get something usable out to the community first.

I hope to get this stuff into the new MVC Contrib project as well.

Whats next, maybe some form helpers, templates, and a base controller to make working with the views and actions a little easier.