【问题标题】:Dynamic default parameter in routing table路由表中的动态默认参数
【发布时间】:2019-05-16 15:39:52
【问题描述】:

我正在为我的应用程序配置路由表,并希望捕获未指定语言的路由并重定向到指定语言页面。现在我只在RouteConfig中找到了映射路由的解决方案,但问题是Initialize()只调用了一次,默认参数只计算了一次。

[InitializableModule]
[ModuleDependency(typeof(ServiceContainerInitialization))]
public class RouteConfig : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        LocalizationService localizationService = LocalizationService.Current;
        var currentLanguage = localizationService.GetString($"/locale/{ContentLanguage.PreferredCulture.Name}", ContentLanguage.PreferredCulture.Name);

        RouteTable.Routes.MapContentRoute(
            "EpiSeverRegisteredRoute",
            "{language}/{node}/{action}",
            new { language = currentLanguage, action = "index" });
    }

    public void Uninitialize(InitializationEngine context) {
    }

    public void Preload(string[] parameters) { }
}

【问题讨论】:

  • 您有想要重定向到的特定语言吗?您的网站支持多少种语言?
  • 现在我的网站支持 6 种语言,但将来可能会添加更多。我想重定向到一种特定的语言,它来自于本地化服务。

标签: c# asp.net-mvc routing episerver


【解决方案1】:

我通过在我的 Global.asax 文件中添加 Application_BeginRequest(Object, EventArgs) 方法解决了我的问题,我在其中对本地化服务进行了相同的工作,并使用指定的另一种语言重定向到路由。现在这个方法在每个请求的开始都会被调用,对我来说并不是 100% 好,但它正在工作。

https://docs.microsoft.com/en-us/dotnet/api/a4swift_mrsr.global.application_beginrequest?view=bts-swift-dotnet

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 2016-08-21
    • 2021-01-08
    • 2020-05-17
    • 2019-02-06
    • 1970-01-01
    相关资源
    最近更新 更多