Asp.net mvc ASP.NET MVC Actionlink返回空链接
我有这个问题,搜索过谷歌和StackOverflow,但似乎我找不到解决办法 我在Global.asax.cs中绘制了以下路线Asp.net mvc ASP.NET MVC Actionlink返回空链接,asp.net-mvc,asp.net-mvc-routing,actionlink,Asp.net Mvc,Asp.net Mvc Routing,Actionlink,我有这个问题,搜索过谷歌和StackOverflow,但似乎我找不到解决办法 我在Global.asax.cs中绘制了以下路线 routes.MapRoute( "posRoute", // Route name "pos/{guid}", // URL with parameters new { controller = "Pos", action = "Index", guid = UrlParameter.Optional } // Parame
routes.MapRoute(
"posRoute", // Route name
"pos/{guid}", // URL with parameters
new { controller = "Pos", action = "Index", guid = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"foxRoute", // Route name
"fox/{guid}", // URL with parameters
new { controller = "Fox", action = "Index", guid = UrlParameter.Optional } // Parameter defaults
);
我想与HTML助手Actionlink建立一个链接,但它始终返回一个空链接
@Html.ActionLink("Proceed", "device")
返回
<a href="">Proceed</a>
@Html.ActionLink("Proceed", "device", "Fox" , new { guid = "test" })
@ActionLink(“继续”,“设备”,“福克斯”,新的{guid=“test”})
返回
<a href="" guid="test">Proceed</a>
预期结果如下:
<a href="/fox/index/test">Proceed</a>
或者更好
<a href="/fox/test">Proceed</a>
尝试重载
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
Object routeValues,
Object htmlAttributes
)
因此,您的代码将是
@Html.ActionLink("Proceed", "device", "Fox" , new { guid = "test" },null)
如果要传递任何HTML属性,如CSS class/ID of element,可以用该属性替换最后一个参数calue(在本例中为null)
还要确保在特定管线下方有通用管线定义
routes.MapRoute(
"posRoute",
"pos/{guid}",
new { controller = "Pos", action = "Index",
guid = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"foxRoute", // Route name
"fox/{guid}", // URL with parameters
new { controller = "Fox", action = "Index",
guid = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute("Default","{controller}/{action}/{id}",
new { controller = "Home", action = "Index",
id = UrlParameter.Optional })
试试过载
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
Object routeValues,
Object htmlAttributes
)
因此,您的代码将是
@Html.ActionLink("Proceed", "device", "Fox" , new { guid = "test" },null)
如果要传递任何HTML属性,如CSS class/ID of element,可以用该属性替换最后一个参数calue(在本例中为null)
还要确保在特定管线下方有通用管线定义
routes.MapRoute(
"posRoute",
"pos/{guid}",
new { controller = "Pos", action = "Index",
guid = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"foxRoute", // Route name
"fox/{guid}", // URL with parameters
new { controller = "Fox", action = "Index",
guid = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute("Default","{controller}/{action}/{id}",
new { controller = "Home", action = "Index",
id = UrlParameter.Optional })
这也给了我“你有通用的路线作为最后一个在全球。asax其他这两个吗?”Jurgenstilart:酷。甚至我也在我的回答中加入了这一点这也给了我“你有通用的路线作为最后一个在全球。asax其他这两个吗?”Jurgenstilart:酷。甚至我也在我的回答中加入了这一点如果您想针对特定的路由,使用Html.RouteLink而不是ActionLink可能是一个不错的选择。如果您正在使用区域并试图链接到另一个区域中的控制器,请执行
@Html.ActionLink(“继续”、“设备”、“福克斯”、新的{area=“Areaname”、guid=“test”},null)
。如果您想以特定路由为目标,使用Html.RouteLink而不是ActionLink可能是一个不错的选择。在我的情况下,我没有在RouteValue中指定区域。如果您正在使用区域并试图链接到另一个区域中的控制器,请执行@Html.ActionLink(“继续”、“设备”、“福克斯”、新建{area=“Areaname”、guid=“test”},null)
。