C# ASP.NET Core 3 MVC端点路由和路由本地化

C# ASP.NET Core 3 MVC端点路由和路由本地化,c#,localization,asp.net-core-mvc,asp.net-mvc-routing,C#,Localization,Asp.net Core Mvc,Asp.net Mvc Routing,我开始处理端点路由和url转换,但我想我遗漏了一些东西 我的解决办法是 我从中的例子开始 在地址栏中编写url的工作和预期的一样,但我注意到锚标记根本不会生成url 布局示例 @{ Dictionary<string, string> routePol = new Dictionary<string, string>(); routePol.Add("language", "pl"); routePol.A

我开始处理端点路由和url转换,但我想我遗漏了一些东西

我的解决办法是

我从中的例子开始

在地址栏中编写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>
@{
Dictionary Route波尔=新字典();
添加(“语言”、“pl”);
routePol.Add(“控制器”、“主页”);
添加(“行动”、“索引”);
}

通过修复,我添加了一个有用的组件,用于使用route value进行文化更改,希望这对所有遇到与我相同问题的人都有用。

博客向我们展示了如何在发送请求时翻译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>
@{
Dictionary Route波尔=新字典();
添加(“语言”、“pl”);
routePol.Add(“控制器”、“扎莫维尼亚”);
routePol.Add(“行动”、“列表”);
}
@*控制器名称和操作名称应为TranslationDatabase中的键名称
不是值名称*@
波兰
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}");

        });
app.UseEndpoints(端点=>
{
endpoints.MapControllerRoute(
名称:“默认”,
模式:“{language=pl}/{controller=orders}/{action=list}”);
endpoints.MapDynamicControllerRoute(
“{language=pl}/{controller=orders}/{action=list}”);
});

它将生成url:
/pl/zamovienia/lista
,当您单击该链接时,它将进入
orders/list
action

对于调试如何验证路由或记录错误,您可以使用源代码进行调试以检查。仍然不起作用。更新了主语言为“en”的解决方案,主端点语言为“en”。我只留下了一种替代语言,并更新了布局中的链接。Href字段在所有链接上仍然为空。很抱歉我错过了要发布的内容(Startup.cs),请检查我的答案。天哪,你说得对,问题是默认路径。除此之外,我确认,即使不使用主语言以外的其他语言的翻译路线,路线构建也可以工作。也就是说,这项工作
{Dictionary routeItCtrlAct=new Dictionary();routeItCtrlAct.Add(“language”,“it”);routeItCtrlAct.Add(“controller”,“home”);routeItCtrlAct.Add(“action”,“index”);}指示意大利语(使用实际控制器动作构建的路线)
@Rena@albe嘿,伙计们,我有以下路由设置:
endpoints.mapcontrolleroute(名称:“solver_cultured”,模式:{culture=en}/solve/{*equation}),默认值:new{Controller=“solve”,action=“SolveOne”})但我无法将路由转换为DE:
{culture=DE}/berechnung/{*equation}
这只适用于显式路由模式;如果您还想翻译querystring,这是可能的,但是您必须显式地将translation transformer类中每个可能的路由参数(也是querystring参数)映射为键,然后为它们提供翻译字典。保持一个简单的lang/controller/action路由,然后使用querystring参数
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapDynamicControllerRoute<TranslationTransformer>(
                    "{language=pl}/{controller=orders}/{action=list}");
            });
@{
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>
app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{language=pl}/{controller=orders}/{action=list}");
            endpoints.MapDynamicControllerRoute<TranslationTransformer>(
                "{language=pl}/{controller=orders}/{action=list}");

        });