【问题标题】:Get url for custom route in asp.net mvc在asp.net mvc中获取自定义路由的url
【发布时间】:2016-03-22 17:14:36
【问题描述】:

在我的 ASP.Net mvc 应用程序中,我已将 startup.cs 中的默认路由注册更改为

routes.MapRoute(
    name: "default",
    template: "{customer}/{language=fi-FI}/{controller=Home}/{action=Index}/{id?}");

因此,所有路线都将包含客户 ID 和当前语言。自定义段匹配负责将客户和语言信息放入新 url,因此当请求的 url 是 /customer1/fi-fi/somecontroller/someaction 时,任何生成的 url 都像这样

<a asp-controller="othercontroller" asp-action="otheraction">Some action</a>

将在其中正确生成客户和语言代码。

问题是,我应该如何生成 url 以便我可以指定客户和语言代码而无需进行字符串连接?我需要这个,例如在更改语言的链接中。

我试过了

<a asp-controller="somecontroller" asp-action="someaction" asp-route="default" asp-route-customer="customer2" asp-route-language="en-us">Some action in other language and other customer</a>

但这表示我无法在定义 asp-route 时指定操作和控制器,如果我远程 asp-route 则没有任何变化。

【问题讨论】:

  • 我在这个项目中有类似的东西支持文件夹租户,github.com/joeaudette/cloudscribe 我认为你需要为 {customer} 实现路由约束,就像我为 SiteFolder 所做的那样。查看示例 web 应用项目中的启动

标签: asp.net-mvc routes


【解决方案1】:

我通过使用@Url.RouteUrl 得到了这个工作,例如

@Url.RouteUrl("default", new { customer = "someothercustomer", language = "someotherlanguage", controller = "somecontroller", action = "someaction" })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 2011-04-02
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多