【发布时间】:2014-03-14 08:07:37
【问题描述】:
我在路由配置中声明了两条路由,如下所示:
routes.MapRoute(
name: "SpecificAction",
url: "{controller}/{action}/{id}-{id2}",
defaults: new { controller = "Main", action = "SpecificAction", id = UrlParameter.Optional, id2 = UrlParameter.Optional }
);
routes.MapRoute(
name: "DefaultNoParams",
url: "{controller}/{action}/",
defaults: new { controller = "Main", Action = "Index" },
namespaces: new string[1] { "Project.Controllers" }
);
我有两个如下所示的控制器操作:
[HttpGet]
public ActionResult TheAction()
{
}
[HttpPost]
public ActionResult TheAction([ModelBinder(typeof(SpecificModelBinder))] SpecificModel model)
{
}
我想要在我的视图中指向这些操作中的第一个的链接,所以我使用Url.Action 生成一个链接:
<a href="@Url.Action("TheAction", "Main")">The Action</a>
但是,这会输出 url http://site/Main/TheAction/-(注意最后的破折号,这似乎表明我的 SpecificAction 路由正在被使用。
有什么方法可以让我用特定的路线拨打Url.Action 吗?或者有没有其他方法可以避免这个破折号出现在网址中?
【问题讨论】:
-
路由表不影响
Url.Action如何呈现一个URL...所以你的链接呈现为<a href="http://site/Main/TheAction/-">? -
@ChrisHardie 是的,我只是想避免这种情况。
-
奇数...当您将此添加到您的视图时呈现的内容:
<a href="@Url.Action("TheAction")">The Action</a> -
@ChrisHardie 同样...
-
http://site来自哪里?
标签: c# .net asp.net-mvc asp.net-mvc-4 routes