MVC 自定义路由和本地化

发布于 2024-12-25 14:05:58 字数 1128 浏览 3 评论 0原文

好吧,伙计们,我已经被困了至少两天了,我在谷歌上搜索并查看了我能找到的所有其他堆栈文章。

我只想检查路由中的第一个参数是否是有效的区域性名称,以及是否在执行之前修改 url/路由以包含区域性值。

有这么难吗?!我认为不应该。

我可以修改所有其他控制器继承的基本控制器类中的内容,并检查那里,然后修改所有路由数据并将其发送。问题出在我的其他一些自定义路线上。

好的,这就是我的代码的问题,以及为什么我认为在执行任何控制器之前最容易做到这一点。

 routes.MapRoute(
            "Localization",
            "{lang}/{controller}/{action}",
            new { lang="en-US",controller="Home",action="Index"}
            );
        routes.MapRoute(
           "Solutions",
           "{lang}/Solutions/{controller}/{action}",
           new { lang="en-US", controller = "WhatWeDo", action = "Index"}
          );
        routes.MapRoute(
            "Default", // Route name
            "{lang}/{controller}/{action}/{id}", // URL with parameters
            new { lang = "en-US", controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

因此,如果我尝试修改内容,我会遇到某些解决方案的讨厌的 /solutions/ 自定义路由,而对于其他答案,它需要自定义重定向(产生永久移动的错误),并检查某些参数是否为空。

有什么方法可以提取路由数据并修改 global.asax 文件中的路由/url?那里是最简单的,我真的很想把它放进去,这样它就一直在那里,我就可以正常路由。

我可能没有说清楚,但我有点沮丧。有什么建议吗?

Okay guys, so I've been stuck for at least 2 days, with googling and checking out all other stack articles I could find.

I just want to check to see if the first parameter in my route is a valid culture name, and if it isn't modify the url/route before execution to include a culture value.

Is that so hard?! I don't think it should be.

I could just modify things in a basecontroller class, from which all other controllers inherit, and check there and then modify all route data and send it on its way. The problem lies with some other custom routes I have.

Okay, so here's the problem with my code, and why I think it would be easiest to do before execution of any controllers.

 routes.MapRoute(
            "Localization",
            "{lang}/{controller}/{action}",
            new { lang="en-US",controller="Home",action="Index"}
            );
        routes.MapRoute(
           "Solutions",
           "{lang}/Solutions/{controller}/{action}",
           new { lang="en-US", controller = "WhatWeDo", action = "Index"}
          );
        routes.MapRoute(
            "Default", // Route name
            "{lang}/{controller}/{action}/{id}", // URL with parameters
            new { lang = "en-US", controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

So, if I try to modify things, I run into this nasty /solutions/ custom route for certain solutions, and for other answers it requires a custom redirect (Yielding a permanently moved error), and checking to see if certain parameters are null.

Is there any way to extract the route data and modify my route/url in the global.asax file? It would be easiest there, and I really would like to just plop it in so that it's always there, and I can just route normally.

I'm probably not making a whole lot of sense, but I'm a bit frustrated. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2025-01-01 14:05:58

我不完全确定你想要实现什么,是在路线中“强制”语言吗?

您可以尝试编写自己的 RouteHandler 来处理事情吗?

我在我的博客上写了类似的内容。

I am not totally sure about what you are trying to achieve, is it to "enforce" language in routes?

You could try writing your own RouteHandler to handle things?

I wrote about something similar on my blog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文