I’m trying to map certain routes so that auto generated Urls will look like
Admin/controller/action/param for both of these code blocks,
@Url.Action("action","controller",new{id="param"}) and
@Url.Action("action","controller",new{type="param"})
What I did was the following in the area registration,
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index",
id = UrlParameter.Optional },
new string[] { "namespaces" });
context.MapRoute(
"Admin_type",
"Admin/{controller}/{action}/{type}",
new { action = "Index",
type = UrlParameter.Optional },
new string[] { "namespaces" });
when parameter name is id, url generated is as expected, but when parameter name is type, instead of controller/action/typevalue, it generates something like controller/action/?type=typevalue
Is there a way to generate the url something like controller/action/typevalue keeping the generator behaviour for Admin_default route intact?
That happens because first route is used to map the url (id is optional).
You could try adding some constraints to your routes. I’m guessing your id parameter is an integer and type parameter is a string. In that case you can try with this routes:
You can find more info on route constraints here.