Search code examples
c#asp.net-mvcroutesepiserver

Dynamic default parameter in routing table


I'm configuring route table for my application, and want to catch routes without language specified and redirect to specified with language page. Now I only found solution in mapping routes in RouteConfig, but problem is that Initialize() is called only once and default parameter is counted only once.

[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) { }
}

Solution

  • I solved my problem by adding Application_BeginRequest(Object, EventArgs) method in my Global.asax file, where i did the same work with localization service and redirect to route with another language specified. Now this method is invoked in begin of every request, and it's not 100% good, as for me, but it's working.

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