首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于asp.net核的自定义语言代码路由问题

基于asp.net核的自定义语言代码路由问题
EN

Stack Overflow用户
提问于 2020-04-01 14:26:30
回答 1查看 443关注 0票数 0

我试图在使用asp.net核心3.1的控制器之前添加一个自定义语言代码,并且语言代码不是C#上的标准文化代码。

例如。

en =英语tch =中文

因此,其结果应当是:

代码语言:javascript
复制
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进行路由

代码语言:javascript
复制
  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" });
            });

下面是我的博客控制器类:

代码语言:javascript
复制
      [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,只是想知道是否有任何设置/编码错误.

有人能给我一些建议吗?谢谢

EN

回答 1

Stack Overflow用户

发布于 2020-04-01 14:33:36

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60973201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档