我试图在使用asp.net核心3.1的控制器之前添加一个自定义语言代码,并且语言代码不是C#上的标准文化代码。
例如。
en =英语tch =中文
因此,其结果应当是:
localhost/en -> Go to Home page in English
localhost/tch -> Go to Home page in Chinese
localhost/ -> Should redirect to localhost/en
also, there are other controller that need to include the language code as well
localhost/en/blog -> Go to blog Listing page in English
localhost/blog -> Redirect to localhost/en/blog and Go to blog Listing page
localhost/en/blog/{id} -> Go to blog detail page in English
localhost/blog/{id} -> Redirect to localhost/en/blog/{id} and Go to blog detail page
localhost/en/event -> Go to event Listing page in English
localhost/event -> Redirect to localhost/en/event and Go to event Listing page
localhost/en/event/{id} -> Go to event detail page in English
localhost/event/{id} -> Redirect to localhost/en/event/{id} and Go to event detail page在startup.cs上,我使用UseEndpoints进行路由
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "others",
pattern: "{language}/{controller}/{id?}",
defaults: new { action = "Index" });
endpoints.MapControllerRoute(
name: "default",
pattern: "{language}/{controller=Home}",
defaults: new { controller = "Home", action = "Index" });
});下面是我的博客控制器类:
[Route("{language}/blog")]
[Route("blog")]
public async Task<IActionResult> Index(string language)
{
if (language == null || (!language.Equals("en") && !language.Equals("tch")))
return RedirectToAction("Index", new { language = "en" });
.......
}如果我输入localhost/en/blog并且语言参数可以从RedirectToAction捕获语言,那么上面的代码可以正常工作,但是当我试图使用时,语言变成了查询字符串localhost/blog? language =en,只是想知道是否有任何设置/编码错误.
有人能给我一些建议吗?谢谢
发布于 2020-04-01 14:33:36
https://stackoverflow.com/questions/60973201
复制相似问题