Asp.net mvc MVC 5路由,参数为@Url.RouteUrl

Asp.net mvc MVC 5路由,参数为@Url.RouteUrl,asp.net-mvc,routing,asp.net-mvc-5,asp.net-mvc-routing,Asp.net Mvc,Routing,Asp.net Mvc 5,Asp.net Mvc Routing,我有一个已应用路由属性的ActionResult: [Route("myproduct/{productID}", Name = "myproduct")] [MvcSiteMapNode(Title = "Products", ParentKey = "products", Key = "myproducts")] public ActionResult myproducts(int productID) ... 我正在尝试通过RouteUrl链接到视图: <a href="@Url.

我有一个已应用路由属性的ActionResult:

[Route("myproduct/{productID}", Name = "myproduct")]
[MvcSiteMapNode(Title = "Products", ParentKey = "products", Key = "myproducts")]
public ActionResult myproducts(int productID) ...
我正在尝试通过RouteUrl链接到视图:

<a href="@Url.RouteUrl("myproducts", @Model.myproducts[i].productID)">Buy</a>
尝试:

并改变你的行动路线,如:

[Route("myproducts/{productID}", Name = "myproducts")]
[MvcSiteMapNode(Title = "Products", ParentKey = "home", Key = "myproducts")]
public ActionResult myproducts(int productID) {

}
尝试:

并改变你的行动路线,如:

[Route("myproducts/{productID}", Name = "myproducts")]
[MvcSiteMapNode(Title = "Products", ParentKey = "home", Key = "myproducts")]
public ActionResult myproducts(int productID) {

}

我可以看到几个问题:

  • 未正确传递参数,因为您直接将productId作为方法的第二个参数传递。您应该正确地传递它(并小心使其与路由中的参数名称匹配),例如使用匿名对象:

    @Url.RouteUrl("myproduct", new { productID = Model.myproducts[i].productID })
    
  • 属性中的路由名称和@Url帮助器中的名称不同(请参见myproductVsmyproducts):


    • 我可以看到几个问题:

      • 未正确传递参数,因为您直接将productId作为方法的第二个参数传递。您应该正确地传递它(并小心使其与路由中的参数名称匹配),例如使用匿名对象:

        @Url.RouteUrl("myproduct", new { productID = Model.myproducts[i].productID })
        
      • 属性中的路由名称和@Url帮助器中的名称不同(请参见myproductVsmyproducts):


      您能给我们看一下您的路线配置吗?我怀疑这是一个如何定义路线的位置问题。i、 默认路由为{controller}/{action}/{id}(可选)。这些占位符名称不是绝对的。但是你有{productId},这可能会打乱你的路线,这就是为什么你没有得到任何东西。在浏览器中导航到/products/myproducts/45时会发生什么情况?我怀疑是404。我已经添加了路由配置。我得到的每一个网址衍生404。我也尝试过对url进行硬编码,而不是每次都使用@url标记和404。您能给我们展示一下您的路由配置吗?我怀疑这是一个如何定义路线的位置问题。i、 默认路由为{controller}/{action}/{id}(可选)。这些占位符名称不是绝对的。但是你有{productId},这可能会打乱你的路线,这就是为什么你没有得到任何东西。在浏览器中导航到/products/myproducts/45时会发生什么情况?我怀疑是404。我已经添加了路由配置。我得到的每一个网址衍生404。我还尝试过对url进行硬编码,而不是每次都使用@url标记和404。谢谢-更改了我传递参数“new{productID=Model.myproducts[I].productID}”的方式。谢谢-更改了我传递参数“new{productID=Model.myproducts[I].productID}”的方式。
      [Route("myproducts/{productID}", Name = "myproducts")]
      [MvcSiteMapNode(Title = "Products", ParentKey = "home", Key = "myproducts")]
      public ActionResult myproducts(int productID) {
      
      }
      
      @Url.RouteUrl("myproduct", new { productID = Model.myproducts[i].productID })
      
      [Route("myproduct/{productID}", Name = "myproduct")]
      @Url.RouteUrl("myproducts", @Model.myproducts[i].productID)