【问题标题】:ASP.NET Core 3 MVC endpoint routing and localization by routeASP.NET Core 3 MVC 端点路由和按路由本地化
【发布时间】:2020-02-23 23:05:42
【问题描述】:

我开始搞乱端点路由和 url 翻译,但我认为我错过了一些东西。

我的解决方案是here

我从this blog post中的例子开始

在地址栏中写入 url 按预期工作,但我注意到锚标记根本不生成 url。

布局示例

     @{ 
         Dictionary<string, string> routePol = new Dictionary<string, string>();
         routePol.Add("language", "pl");
         routePol.Add("controller", "Home");
         routePol.Add("action", "Index");
    }
    <a asp-all-route-data="routePol">Poland</a>

应该生成

<a href="test/pl/Home/Index">Poland<a>

结果

<a href >Poland<a>

主端点

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapDynamicControllerRoute<TranslationTransformer>(
                    "{language=pl}/{controller=orders}/{action=list}");
            });

我不明白出了什么问题。我阅读了端点路由,当提供的值生成不存在的路由时,锚标记不会生成 url,但上面的路由应该是有效的。

如果可能的话,有没有办法调试如何验证路由或记录错误?

编辑:问题现已解决,感谢 Rena。 我更新了my solution 并进行了修复,并添加了一个有用的组件,用于使用路线值改变文化,希望这对遇到与我相同的问题的每个人都有用。

【问题讨论】:

  • 对于如何调试路由验证或记录错误,您可以使用源代码进行调试。

标签: c# localization asp.net-core-mvc asp.net-mvc-routing


【解决方案1】:

该博客向我们展示了如何在您发送请求时翻译 url。但在您的情况下,您只想呈现 url,它应该添加路由模板以匹配它。

这是一个简单的解决方法,如下所示:

1.查看:

@{
Dictionary<string, string> routePol = new Dictionary<string, string>();
routePol.Add("language", "pl");
routePol.Add("controller", "zamowienia");
routePol.Add("action", "lista");    
}
@*controler name and action name should be the key name in TranslationDatabase 
not the value name.*@
<a asp-all-route-data="routePol">Poland</a>

2.Startup.cs:

app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{language=pl}/{controller=orders}/{action=list}");
            endpoints.MapDynamicControllerRoute<TranslationTransformer>(
                "{language=pl}/{controller=orders}/{action=list}");

        });

它会生成url:/pl/zamowienia/lista,当你点击链接时,它会进入orders/list动作

【讨论】:

  • 还是不行。更新解决方案,主要语言为“en”,主要端点语言为“en”。我只留下了一种替代语言,并更新了布局中的链接。所有链接上的 Href 字段仍然为空。
  • 对不起,我错过了一些东西(Startup.cs)要发布,请检查我的答案。
  • 天哪,你是对的,问题是默认路由。通过此添加,我确认即使不使用除主要语言之外的其他语言的翻译路线,路线构建也能正常工作。 IE。这工作@{ Dictionary&lt;string, string&gt; routeItCtrlAct = new Dictionary&lt;string, string&gt;(); routeItCtrlAct.Add("language", "it"); routeItCtrlAct.Add("controller", "home"); routeItCtrlAct.Add("action", "index"); } &lt;a asp-all-route-data="routeItCtrlAct"&gt;Indice italiano (route builded with real controller-action)&lt;/a&gt;
  • @Rena @albe 嘿伙计们,我有这个路由设置:endpoints.MapControllerRoute( name: "solver_cultured", pattern: "{culture=en}/solve/{*equation}", defaults: new { Controller = "Solve", action = "SolveOne" }); 但我无法将路由转换为 DE:{culture=de}/berechnung/{*equation}
  • 恕我直言,这只适用于明确的路由模式;如果您还想翻译查询字符串,这可能是可能的,但是您必须将翻译转换器类中的每个可能的路由参数(也是查询字符串参数)显式映射为键,然后为它们提供翻译字典。保留一个简单的 lang/controller/action 路由,然后使用查询字符串参数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-31
  • 2018-04-15
  • 1970-01-01
  • 2011-08-11
  • 1970-01-01
  • 1970-01-01
  • 2019-06-13
相关资源
最近更新 更多