ASP.NET MVC Preview 2 is released and you can download it from here.
In this CTP, A lot of changes happened in the MVC framework such as:
1- When you define a controller action method you don't have to put the [ControllerAction] all you have to do is to define the method as public and it will be part of the contract, Okay but what if I wanted to define a non action method, well now you have to put an attribute J [NonAction].
2- Another thing is the {*catchall} The new * which allows you pass anything from the * to the end of the URL as a parameter for the action. And this is actually what I'm going to talk about in this post. But you can find a lot of stuff about the ASP.NET MVC Framework Preview 2 in Scott Hanselman Videos here.
In my previous post I bloged about ASP.NET MVC & JQuery, I was calling an action method which takes as a parameter the name of the category (In the Northwind database) to get the products for that category.
Well, the thing is some of the categories name contains "/" which as you already know is used in the URL, so it didn't work. I overcome this issue by using Query String to pass in the parameter to the action method like that:
var url = '/Products/GetProducts?category='+ categoryName;
But now with the new {*catchall} route rule it can be done.
routes.Add(new Route( "Products/GetProducts/ {*category}",new MvcRouteHandler())
{
Defaults = new RouteValueDictionary (new
{
controller = "Products",
action = "GetProducts"
}),
});
As you can see I used the {*category} not {*catchall} so catchall is not a keyword the * is the important part. But the important thing is whatever name you put after the {*} you must use the same name for the action method parameter.
For example, in the previous snippet I used:
"Products/GetProducts/{*category}
So in my Get Products action method I must pass in a category parameter like that:
public void GetProducts(string category)
{
}
The full source is attached.
P.S: To convert an MVC Application from Preview 1 to Preview 2 there is a lot of stuff and I mean "a lot" that you will have to change to get your application up and running and most of the changes will not be in your code. So if your application is a small one my advice is that create a new application and just copy & paste your code in to it and now the changes that you will have to make is in your code J
But if your application big and hard to just copy & paste just follow the steps the release notes here.
Beckham