Framework Madness!

And other adventures C# and …

MVC 2 Preview 2: Strong-Typed Route Links and Other Thoughts

with 2 comments

I’m setting here and looking back at when I last made an entry – September 20th. Been a while. Work as been very busy and I’ve been engrossed in exploring MCV 2 on my own time.  So let’s get to it.

Strong-Typed Route Links

I have been investigating this heavily the past week or two. Based on past experience I have found the notion of un-typed, “magic string” routes to be a bit of insanity. So, with it appearing that strong-typed route links are no longer on the MVC 2 roadmap finding a solution was paramount. I’ve seen two nice solutions thus far.

The first thing I found was  David Ebbo’s T4MCV t4 template. It’s a very nice piece of work. You’ll find it on codeplex along with the the MCV 1 source code. Here’s the link:

Setting up T4MVC was quite easy. Just drop it into your project root and go. It will create strong-typed routes, links to scripts and much more. It’s a very nice piece of T4 template work. However, I personally had a few concerns. The biggest is the method you call to create the route is not the same method in your controller, it’s an overload that returns the values in a special ActionResult object. Now – you might be thinking – what’s the problem? Let’s say I want to use my own attributes to control how parameters are formatted in making a route. (Specifically, I wanted to format a DateTime object because the default ‘ToString’ representation is not route friendly at all. ) Since it’s an overload the attributes are not there, T4MVC does not copy them, and for met to put them there and use them takes a lot of T4 hacking that I really don’t want to get to (no Intellisense).  So while I really respect the work – it wasn’t what something I could easily tool to my liking. So with great regret, I pulled out T4 MVC based and went solution hunting again. What was I after? I was looking for an expression (as in lambda expression) based solution. And then I stumbled on on Goran Gagic’s sample. Here’s the link:

Curiously, it’s the only blog entry he has. But it’s a gold mine. He has a very nicely written sample solution ( even saith  The Haack ) that serves as a foundation for building a strong-typed route link system, and also serves as an well documented insight into using lambda expression. So I’ve taken the sample and worked in the ideas I wanted to implement and will be posting a full solution in the next few days. But here is a bit of my thinking on why I wanted this kind of solution in the first place.

The main conclusion I had when trying to work with T4MVC is that while a controller action method can serve as the basis for making a strong-typed route link there needs to be tools for formatting the route derived from the method. And after considering several avenues I decided on attributes directly applied to the controller action method. Why? They’re already being used to nice effect already. For example the ‘ActionName’ attribute allows you to have a different route name (say if want to use a logical method name but also want the same method to be the controller’s ‘Index’ action). In MVC 2 the ‘DefaultValue’ attribute allows you set a default for parameters not passed along in the route. (This is a nice workaround for .net 3.5 and won’t be necessary for 4.0 as C# 4.0 supports optional parameters with default values). And the ‘Bind’ attribute allows you to choose a prefix for a parameter and even set includes/excludes on complex objects. So creating a ‘RouteValue’ attribute is a logical extension. Here’s an example (this method  exists in a controller class named ‘ArticleVisualController’ as you will see later ):

   1: public ActionResult Date(

   2: [DefaultValue(typeof(DateTime),"2000-01-01"),RouteValue(typeof(DateTime), "2000-01-01",Format="yyyy-MM")] DateTime id,

   3: [DefaultValue(0),RouteValue(Default=0)] int index)

   4: {

   5:     //method body here

   6: }

In this case the RouteValue attribute is supplying two kinds of detail. First for the ‘id’ parameter it’s supplying the format of the DateTime to be used in the route. Secondly  on the ‘id’ and ‘index’ parameters it’s providing a default value, and if the parameter value from the called method expression matches the default it will not be added to the RouteValues dictionary. Here are examples of creating an action link for the method above:

   1: <%
   1: = Html.ActionLink<ArticleVisualController>("Date", a => a.Date(new DateTime(2009,09,01),0))



   3: <%
   1: = Html.ActionLink<ArticleVisualController>("Date", a => a.Date(new DateTime(2009,09,01),1))


Here are the corresponding routes created:

   1: <a href="/testing1/Date/2009-09">Date</a>


   3: <a href="/testing1/Date/2009-09?index=1">Date</a>


You can see in both resulting routes the DateTime is formatted as requested (ModelBinding ‘magic’ will ensure it’s a real DateTime object when the controller action method is called in the request) and in the first route the ‘index’ parameter is not added as the value matches the default.

Here is a list of things I will looking to support:

  1. Route parameter formatting
  2. Route parameter default values
  3. Using the ‘Bind’ attribute ‘prefix’, ‘include’ and ‘exclude’ properties
  4. Using the ‘ActionName’ attribute
  5. Support for complex parameter types
  6. And a few others …

I have about 75% of this work done and need to polish for a sample release hopefully next weekend.

Thank you Goran Gagic! You have shown the way!

Lambda Expression Compile Caching Namespace

Honest! The API has it’s own name space and supports several expression types. It looks like this was made to speed the DataAnnotations work, but it’s all internal. Having an open API would help. Wondering if that’s in the plan (or could be) for the beta. Lots of MVC blog posts seem to be leaning towards expression usage and having Lambda Expression compile caching out of the box would be helpful.

And on that matter, is Lambda Expression compile caching a part of .Net 4.0? If not – curious to know why?

Client Side Validation

It works! All you have to do is link to the jQuery and Ajax scripts and it works. It is not implemented when using ‘Ajax.BeginForm’ – and this is probably a good idea since you never know what’s coming back and avoids wire up problems.

Areas and Related Tips

Nicely done. I am hoping there will be an ‘areas’ folder out of the box added to new projects to the beta. Why? See the first tip below.

  1. Area view compilation problems. If you run into this just copy the ‘web.config’ from the normal ‘View’ folder and place it under the ‘area’ folder. This will give your views the appropriate compilation settings and prevents them from being viewed by direct url call.
  2. Controller namespace in ‘Areas’. If you move a controller from the normal “Controllers’ to a custom area ‘Controllers’ folder be sure to update the namespace. If not it won’t be available and you’ll be stuck with a 404 error.

Lambda Expression Programming Insights?

Does anybody know of any good resources for this? What I don’t mean by this is how to write expressions for linq – but for documentation, tutorials and/or books on how to do custom programming with them as the strong-typed route link Goran’s sample does. I would love to grok it.


Written by Lynn Eriksen

October 11, 2009 at 2:25 am

Posted in Uncategorized

Tagged with

2 Responses

Subscribe to comments with RSS.

  1. Many of the chicken delicacies are also possible to be
    made by mixing some spices with other daily use items such as coconut, loaf,
    onion, garlic, mushroom, etc. Place plenty of shredded lettuce or salad mix in an over-sized bowl.
    You can place a lettuce leaf on a plate and put a scoop of chicken salad in the middle and serve
    with reduced calorie crackers.

    chicken salad

    July 26, 2013 at 4:32 pm

  2. Whatever your specialty or favored photographic genre may be, photogenic scenes,
    people, places and ideas are all around you almost
    constantly. Take the low-carb tortilla or wrap and put about
    a tablespoon or two of cream on it. If you desire a longer and better quality of life choosing include HEALTHY EATING HABITS into your lifestyle is a smart choice.

    September 16, 2013 at 8:06 am

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: