我有点想在ASP.NET MVC4中获取动作网址。
我正在使用以下razor语法创建链接:
@Html.ActionLink("some text", "Create", "Reports")
我得到的输出是:
<a href="/?action=Create&controller=Reports">some text</a>
我想要的是:
<a href="/Reports/Create">some text</a>
任何想法,我缺少什么?
更新
RouteConfig如下:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// ignoring .aspx, .asmx files
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.IgnoreRoute("{resource}.asmx/{*pathInfo}");
routes.MapPageRoute("DefaultRoute", string.Empty, "~/Default.aspx");
routes.MapPageRoute("MapsRoute", "Map", "~/Default.aspx");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Reports", action = "Index", id = UrlParameter.Optional }
);
}
2ND UPDATE
如果我评论路线配置中的以下行,我会按预期获得网址。知道为什么吗?
routes.MapPageRoute("DefaultRoute", string.Empty, "~/Default.aspx");
routes.MapPageRoute("MapsRoute", "Map", "~/Default.aspx");
答案 0 :(得分:0)
试试这个
@Html.ActionLink("some text", "Create", "Reports",null,null)
尝试添加route.config
routes.MapPageRoute("DefaultRoute", "{def}", "~/Default.aspx");
routes.MapPageRoute("MapsRoute", "{map}", "~/Default.aspx");
actionlink的基类如下:
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
Object routeValues,
Object htmlAttributes
)