使用Url.Action的特定路线

Man*_*eld 3 .net c# asp.net-mvc routes asp.net-mvc-4

我在路由配置中声明了两条路由,如下所示:

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" }
);
Run Code Online (Sandbox Code Playgroud)

我有两个控制器动作,如下所示:

[HttpGet]
public ActionResult TheAction()
{

}

[HttpPost]
public ActionResult TheAction([ModelBinder(typeof(SpecificModelBinder))] SpecificModel model)
{

}
Run Code Online (Sandbox Code Playgroud)

我希望在我的视图中链接到第一个这些操作,因此我使用Url.Action以下方法生成一个:

<a href="@Url.Action("TheAction", "Main")">The Action</a>
Run Code Online (Sandbox Code Playgroud)

但是,这会输出url http://site/Main/TheAction/-(注意末尾的破折号,这似乎表明我的SpecificAction路线正在使用中.

有什么方法可以Url.Action用特定路线打电话吗?或者有没有其他方法可以避免在网址中出现这个短划线?

d.p*_*pov 7

这里有重复的问题

简而言之,使用Url.RouteUrl()intead Url.Action()来获取正确的URL.

在您的情况下,@Html.RouteLink()如果您愿意,甚至可以使用获取整个锚标记.