MVC 5使用@ Url.RouteUrl使用参数进行路由 [英] MVC 5 Routing with parameter using @Url.RouteUrl

查看:95
本文介绍了MVC 5使用@ Url.RouteUrl使用参数进行路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ActionResult,已将路由属性应用于:

I have an ActionResult that I have applied a routing attribute to:

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

我正在尝试通过RouteUrl链接到视图:

I am trying to link to the view via a RouteUrl:

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

生成的html甚至不包含href:

The resulting html does not even include a href:

<a>Buy</a>

如果我删除该参数,它将起作用:

If I remove the parameter it works:

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


<a href="/products/myproducts">Book</a>

我是否尝试错误地添加参数?该文档提出了我的尝试.

Am I trying to add the parameter incorrectly? The documentation suggests what I am attempting.

路由配置:

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 })

并更改您的行动路线,例如:

And change your action route like:

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

}

这篇关于MVC 5使用@ Url.RouteUrl使用参数进行路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆