Asp.net mvc 3 url.操作(控制器、操作)未按预期路由
在我的ASP.NETMVC3中,我使用了以下代码Asp.net mvc 3 url.操作(控制器、操作)未按预期路由,asp.net-mvc-3,razor,routing,href,Asp.net Mvc 3,Razor,Routing,Href,在我的ASP.NETMVC3中,我使用了以下代码 <a href=@url.Action("myController", "myaction")> 我错过什么了吗 谢谢 编辑: 以下是我的路线: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRout
<a href=@url.Action("myController", "myaction")>
我错过什么了吗
谢谢
编辑:
以下是我的路线:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
Url.Action的签名为:
Url.Action(string actionName, string controllerName)
根据您的代码,您的参数顺序不正确,您应该尝试:
@Url.Action("myAction", "myController")
还请记住,要删除控制器的“控制器”部分,例如,如果我有一个带有索引操作的CustomerController,则如下所示:
@Url.Action("Index", "Customer")
第一个参数被解释为动作
@url.Action("myaction","myController")
我也有同样的问题。。。
我的解决方案:
@Html.ActionLink(“链接文本”、“myAction”、“myController”,新的{id=item.id},null)
只需将null添加到最后一个参数。对于默认操作(路由中未定义的操作),正常HTTP GET将自动转到,您只需使用null操作
@Url.Action(null,“Customer”)请包括来自Global.asax.cs的路由-您能否确认上述Url.Action调用完全相同?这通常发生在字符串作为参数传递时,该参数应为
routeValues
objectyes您是对的-但我不确定我们得到的代码是否准确,因为string,string
重载仍然会呈现没有查询字符串的Url。
@url.Action("myaction","myController")