My requirements are to be able to map a custom controller but have it built in that only one action can ever be called. So instead of this:
context.MapRoute(
"RouteName",
"{controller}/{id}",
new {
controller = "MyCustomController",
action = "DefaultAction", <- I want to remove this.
id = UrlParameter.Optional
}
);
I want to do this:
context.MapRoute(
"RouteName",
"{controller}/{id}",
new {
controller = "MyCustomController",
id = UrlParameter.Optional
}
);
And to have it call “DefaultController” by well…default without having to specify declare it in the defaults argument of MapRoute.
Any help?
You can’t do that. Your url doesn’t contain an
{action}token. This means that you absolutely should specify which action you want to invoke in the default values.Put yourself on the place of the routing engine. You see the following request:
Home/123. You look at the route definition and you see{controller}/{id}.Great => now you know that you could instantiate the
HomeControllerbut that’s all you know if an action is not specified as a default value. You have no idea which action to invoke on thisHomeControllerand the story ends here.You throw the following exception to the developer to inform him about the problem with his code:
UPDATE:
You could write a custom extension method that will allow you to omit the default action in the declaration but it will add it behind the scenes:
and then: