我的路线如何使用可选参数使用ASP MVC3 URL的中间? [英] How can my route use optional parameters in the middle of the URL using ASP MVC3?
本文介绍了我的路线如何使用可选参数使用ASP MVC3 URL的中间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想我的网址使用的约定:
I would like my URLs to use the convention:
/{controller}/{id}/{action}
而不是
/{controller}/{action}/{id}
我想建立一个路线如下:
I tried setting up a route as follows:
routes.MapRoute(
"Campaign",
"{controller}/{action}/{id}",
new { controller = "Campaign", action = "Index", id = UrlParameter.Optional }
);
但是,这并不工作,因为我无法做出id参数可选。
But this doesn't work because I am unable to make the id parameter optional.
以下网址做的工作
/campaign/1234/dashboard
/campaign/1234/edit
/campaign/1234/delete
但这些网址不要
/campaign/create
/campaign/indexempty
MVC只是调用首页
两种。我在做什么错了?
MVC just calls Index
for both. What am I doing wrong?
推荐答案
我想你也许需要为这个两个独立的路线。
I think you probably need two separate routes for this.
routes.MapRoute(
"CampaignDetail",
"{controller}/{id}/{action}",
new { controller = "Campaign", action = "Index" }
);
routes.MapRoute(
"Campaign",
"{controller}/{action}",
new { controller = "Campaign", action = "Index" }
);
这篇关于我的路线如何使用可选参数使用ASP MVC3 URL的中间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文