ActionLink 将使用默认或第一个匹配路由生成方法的虚拟路径。因此,您有以下选择
RegisterRoutes
1以Global.asax
更高的优先级添加条目(即在其他路由定义之前):
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"CustomRoute", // Route name
"Something/{controller}/{action}"
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
在这种情况下CustomRoute
将应用于所有匹配的路由,所以这是一个相当全局的变化。
2CustomRoute
以较低的优先级添加(即在其他路由之后)并在视图中定位它:
@Html.RouteLink("MyLink", "CustomRoute", new { controller = "Home", action = "Index" });
3 将您自己的扩展方法写入 HtmlHelper 以提供所需的功能。使用以下代码在您的解决方案中添加一个新文件:
namespace System.Web.Mvc {
{
public static class HtmlHelperExtensions
{
public static MvcHtmlString CustomActionLink(this HtmlHelper htmlHelper, tring linkText, string actionName, string controllerName)
{
return new MvcHtmlString(String.Format("<a href='http://myUrl.com/{0}/{1}'>{2}</a>", controllerName, actionName, linkText));
}
}
}
用法:
@Html.CustomActionLink("LinkText", "Action", "Controller")
4 不要使用 ActionLink helper 的方法,只需在视图中写入您的 URL。