【问题标题】:Sub route in @Url.Action@Url.Action 中的子路由
【发布时间】:2015-09-10 22:52:45
【问题描述】:

我需要有关 asp.net mvc 中子路由的帮助。我需要这条路线

Home/Index/6/Person/Index/8

在我的 RouteConfig 我有这个

routes.MapRoute(
    name: "Prueba",
    url: "Home/Index/{id}/Person/{action}/{personId}",
    defaults: new { controller = "Person", action = "Index", PersonId = UrlParameter.Optional }
);

如果我把这条路由 Home/Index/6/Person/Index/8 放在 URL 浏览器中可以工作,但我需要链接

我不知道如何使用@Url.Action(Action, Controller, new{ id=Value }) 创建这条路线

【问题讨论】:

  • 语法:@Url.Action("Action Name","ControllerName",new {parameterName="parameterValue"}) 在你的情况下:@Url.Action("Index","Person",新的 {id="6",personId="8"})

标签: asp.net asp.net-mvc routes


【解决方案1】:

您可以使用Url.RouteUrlHtml.RouteLink 方法

例如

@Html.RouteLink("Click me", "Prueba", new { id = 1, personId = 3 })

@Url.RouteUrl("Prueba", new { id = 1, personId = 3 })

如果你想在路由中指定不同的操作方法,那么例如new { id = 1, personId = 3, action = "Edit" }

【讨论】:

  • 请注意,personId 参数不是匹配路由所必需的。
【解决方案2】:

你可以使用@Url.Action("Index","Person",new {id="6",personId="8"})

这将生成所需的字符串,但是它将选择符合条件的第一个路由。

语法:@Url.Action("Action Name","ControllerName",new {parameterName="parameterValue"})

参考:

https://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action(v=vs.118).aspx#M:System.Web.Mvc.UrlHelper.Action%28System.String,System.String,System.Web.Routing.RouteValueDictionary%29

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 2014-03-14
    • 2011-12-17
    • 2020-02-01
    相关资源
    最近更新 更多