Url.Action() helper with T4MVC action call optional parameters

May 30, 2012 at 3:16 PM

With the following action signature: 

public virtual ActionResult JsonFeed(int day = 0, int month = 0, int year = 0, string culture = "") {}

and the following route:

routes.MapRoute(
"posts-jsonfeed",
"{culture}/posts/json/{year}/{month}/{day}",new
{
culture = WebSettings.Instance.DefaultCulture,
controller = "Post",
action = "JsonFeed",
day = UrlParameter.Optional,
month = UrlParameter.Optional,
year = UrlParameter.Optional
},
...

T4MVC will create an URL like "/culture/posts/json/0/0/0" when calling :

T4MVC.Post.JsonFeed()

This is something we don't really want. ASP.NET MVC is able to handle the "URL /culture/posts/json" just fine. Actually, the ASP.NET MVC built in Url.Action(action, controller) will skip the optional parameters.

Is there a way we put a setting in the settings file to set the behaviour when optional parameters are present in controller actions?

Coordinator
May 31, 2012 at 7:26 PM

Normally, T4MVC generates a no-param action overload that would make this work. But in this case, it can't do this because all the action params are already optional.

And then once it calls the regular overload, it ends up putting all the values in the route.

I need to think about the situation some more.