Howto automatically add a specific value from current route to all generated links?

前端 未结 1 663
感情败类
感情败类 2021-01-03 06:37

I have site culture in URLs like this:

routes.MapRoute(
    \"Default\", 
    \"{language}/{controller}/{action}/{id}\", 
    languageDefaults, 
    languag         


        
相关标签:
1条回答
  • 2021-01-03 07:17

    It should preserve all values defined in route and present in RouteData automatically, unless you set it to something else. Try to create link without T4MVC or check your route definitions. Something like this works for me just fine:

    routes.MapRoute("Default with language", "{lang}/{controller}/{action}/{id}", new
    {
        controller = "Home",
        action = "Index",
        id = UrlParameter.Optional,
    }, new { lang = "de|fr" });
    routes.MapRoute("Default", "{controller}/{action}/{id}", new
    {
        controller = "Home",
        action = "Index",
        id = UrlParameter.Optional,
        lang = "en",
    });
    

    +

    protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        MvcHandler handler = Context.Handler as MvcHandler;
        if (handler == null)
            return;
    
        string lang = (string)handler.RequestContext.RouteData.Values["lang"];
    
        CultureInfo culture = CultureInfo.GetCultureInfo(lang);
    
        Thread.CurrentThread.CurrentUICulture = culture;
        Thread.CurrentThread.CurrentCulture = culture;
    }
    

    +

    <%: Html.ActionLink("About us", "Detail", "Articles", new { @type = ArticleType.About }, null) %>
    
    0 讨论(0)
提交回复
热议问题