MVC 5 路由参数使用@Url.RouteUrl
MVC 5 Routing with parameter using @Url.RouteUrl
我有一个应用了路由属性的 ActionResult:
[Route("myproduct/{productID}", Name = "myproduct")]
[MvcSiteMapNode(Title = "Products", ParentKey = "products", Key = "myproducts")]
public ActionResult myproducts(int productID) ...
我正在尝试 link 通过 RouteUrl 访问视图:
<a href="@Url.RouteUrl("myproducts", @Model.myproducts[i].productID)">Buy</a>
结果 html 甚至不包含 href:
<a>Buy</a>
如果我删除它有效的参数:
[Route("myproducts", Name = "myproducts")]
[MvcSiteMapNode(Title = "Products", ParentKey = "home", Key = "myproducts")]
public ActionResult myproducts(int productID) ...
<a href="/products/myproducts">Book</a>
我是不是加错了参数?该文档表明了我正在尝试的内容。
路由配置:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
尝试:
@Url.RouteUrl("myproducts", new { productID = Model.myproducts[i].productID })
并改变你的行动路线:
[Route("myproducts/{productID}", Name = "myproducts")]
[MvcSiteMapNode(Title = "Products", ParentKey = "home", Key = "myproducts")]
public ActionResult myproducts(int productID) {
}
我可以看到几个问题:
参数未正确传递,因为您将 productId 直接作为 Url.RouteUrl
方法的第二个参数传递。您应该正确传递它(并且要小心,使其与路由中的参数名称相匹配),例如使用匿名对象:
@Url.RouteUrl("myproduct", new { productID = Model.myproducts[i].productID })
属性中的路由名称和@Url helper中的名称不同(见myproduct vs 我的产品):
[Route("myproduct/{productID}", Name = "myproduct")]
@Url.RouteUrl("myproducts", @Model.myproducts[i].productID)
我有一个应用了路由属性的 ActionResult:
[Route("myproduct/{productID}", Name = "myproduct")]
[MvcSiteMapNode(Title = "Products", ParentKey = "products", Key = "myproducts")]
public ActionResult myproducts(int productID) ...
我正在尝试 link 通过 RouteUrl 访问视图:
<a href="@Url.RouteUrl("myproducts", @Model.myproducts[i].productID)">Buy</a>
结果 html 甚至不包含 href:
<a>Buy</a>
如果我删除它有效的参数:
[Route("myproducts", Name = "myproducts")]
[MvcSiteMapNode(Title = "Products", ParentKey = "home", Key = "myproducts")]
public ActionResult myproducts(int productID) ...
<a href="/products/myproducts">Book</a>
我是不是加错了参数?该文档表明了我正在尝试的内容。
路由配置:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
尝试:
@Url.RouteUrl("myproducts", new { productID = Model.myproducts[i].productID })
并改变你的行动路线:
[Route("myproducts/{productID}", Name = "myproducts")]
[MvcSiteMapNode(Title = "Products", ParentKey = "home", Key = "myproducts")]
public ActionResult myproducts(int productID) {
}
我可以看到几个问题:
参数未正确传递,因为您将 productId 直接作为
Url.RouteUrl
方法的第二个参数传递。您应该正确传递它(并且要小心,使其与路由中的参数名称相匹配),例如使用匿名对象:@Url.RouteUrl("myproduct", new { productID = Model.myproducts[i].productID })
属性中的路由名称和@Url helper中的名称不同(见myproduct vs 我的产品):
[Route("myproduct/{productID}", Name = "myproduct")] @Url.RouteUrl("myproducts", @Model.myproducts[i].productID)