【发布时间】:2013-09-06 02:36:44
【问题描述】:
我有一个像这样的 MVC 控制器操作方法
public ActionResult DoSomething(bool special = false)
{
// process the special value in some special way...
return View();
}
我想使用两个不同的链接访问此操作,它们仅通过特殊标志不同,并且我想将标志作为人类可读的路由值传递。 更准确地说,链接应如下所示:
SomeController/DoSomething
SomeController/DoSomething/Special
目前我已经创建了动作链接:
@Html.ActionLink("Just do it", "DoSomething", "SomeController")
@Html.ActionLink("Do it in a special way", "DoSomething", "SomeController", new { special = true}, null)
这段代码会生成这样的链接:
SomeController/DoSomething/Special
SomeController/DoSomething?special=True
显然,我需要一条特殊路线才能使第二个链接变为SomeController/DoSomething/Special,但我所有的尝试都失败了,因为在一次 MapRoute 尝试中它忽略了我的特殊标志,而在另一次 MapRoute 尝试中它使两个链接都变为 SomeController/DoSomething/Special虽然我没有为第一个 ActionLink 指定特殊的路由值(我猜它只是从路由中获取的)。
将 bool special 映射到 URL SomeController/DoSomething/Special 并使 ActionLink 生成正确链接的正确方法是什么?
【问题讨论】:
标签: asp.net-mvc routing