b.logrythmik

{ independency injection }

A Light-Weight MVC Route Debugging App

I have a huge MVC project that I am working with that has a pretty complex routing schema. Debugging routes gets pretty hairy, so I was delighted to find the RouteDebug assembly that Phil Haack put together. It helped me immensely, but my project takes too long to compile for quick route testing. 

I needed a quick way of creating routes and testing the route values, so I created a super-light-weight small website to use his assembly with nothing else.  You can put your route creation code right into the Global.asax and instantly get the results. I found this super useful and I hope you do too.

 

RouteDebug-Web.zip (12.19 kb)

Expression-Based URLs in MVC - Using a Specific Route with LinkBuilder

In the MVC Future Assembly there lives a super handy class called "LinkBuilder". This class takes an expression and builds a URL using your configured routes.

Creating an extension method in your own project to use this class is pretty straight forward:

public static string Action<TController>(this UrlHelper helper, Expression<Action<TController>> action)

    where TController : Controller

{

    return Microsoft.Web.Mvc.LinkBuilder.BuildUrlFromExpression(helper.RequestContext, helper.RouteCollection, action);

}

You can also use this library to create your own "ActionLink" extensions too. BUT, this isn't why I wrote this article.

I wanted to extend this method to specify an EXACT route to use, as I had a special route to format the URLs and the above method uses the first route it finds that could work - usually the default route.

After struggling for hours trying to parse the expression myself (UGH!), a simple solution presented itself:

public static string Action<TController>(this UrlHelper helper, Expression<Action<TController>> action, string routeName)

    where TController : Controller

{

    var routeCollection = helper.RouteCollection;

 

    // Check to ensure this route exists first. If not, don't filter the routes.

    if (helper.RouteCollection[routeName] != null)
         routeCollection  = new RouteCollection { helper.RouteCollection[routeName] };

 

    return Microsoft.Web.Mvc.LinkBuilder.BuildUrlFromExpression(helper.RequestContext, routeCollection, action);

}

This method filters the RouteCollection down to the exact route you are want to use and passes the filtered collection to the LinkBuilder... ahhh, routing-bliss.

Now you can use it in your views like this:

<%= Url.Action<CredentialsController>(c => c.Edit(credID), "CredentialActions") %>

 

 

Using Policy Injection with ASP.NET MVC and Unity

There are a million and a half articles out there about Policy Injection and the Policy Injection Blocks (from Enterprise Library). Most of them are by my new favorite blogger, David Hayden. If you aren't familiar with Unity, check out these posts first.

The articles span the life-cycle of Unity from before Policy Injection was introduced and some of the actual type names change from article to article, so it wasn't as straight forward as I am going to make it here.

First of all, I didn't want to use a config file - I wanted to configure my policies in the same place I was configuring my dependencies; in the Global.asax code-behind.

It turns out that this is super easy!

Assuming you already have a reference to the Unity library "Microsoft.Practices.Unity", all you need to add is a reference to the "Microsoft.Unity.Interception" library, found in Microsoft Enterprise Library 4.1

You probably already have a place where you are configuring your dependencies:

// Register for Dependency Injection

container.RegisterType<IDataContextProvider, DataContextProvider>();

Now all you need to do is setup the container with an extension that's called 'Interception' then configure each of the Interfaces you want to intercept with Policies. Like so:

// Register for Policy Injection

container.AddNewExtension<Interception>().Configure<Interception>()

    .SetDefaultInterceptorFor<IDataContextProvider>(new TransparentProxyInterceptor());

Now, when you use Unity to resolve your objects, it will detect if any policies are decorated on the interface or the concrete class. If there are policies, Unity creates a transparent proxy object and executes your policies like it does in the Policy Application Blocks. Check out this article to see how to create handlers.

Now you are ready to place your "Policy Attributes" on your classes. Later, I will show you some of my favorite policies I have written for MVC.