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

前提是你 提交于 2019-11-30 15:41:40

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) %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!