ASP.NET MVC5通过单击Html.ActionLink更改语言/文化

ASP.NET MVC5通过单击Html.ActionLink更改语言/文化,asp.net,asp.net-mvc-5,Asp.net,Asp.net Mvc 5,我的ASP.NET MVC5应用程序有问题。我的应用程序可以在浏览器中设置语言/文化(现在只有英语和波兰语(默认设置))。我想让用户通过点击Html.ActionLink来改变语言/文化 我创建了一个类: namespace Guestbook { public static class Click { public static void SetCulture(string name) { Thread.CurrentTh

我的ASP.NET MVC5应用程序有问题。我的应用程序可以在浏览器中设置语言/文化(现在只有英语和波兰语(默认设置))。我想让用户通过点击Html.ActionLink来改变语言/文化

我创建了一个类:

namespace Guestbook
{
    public static class Click
    {
        public static void SetCulture(string name)
        {
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(name);
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
        }

    }
}
我认为:

@Html.ActionLink("PL", "", "Guests", routeValues: null, htmlAttributes: new { onclick = "SetCulture(\"pl\");" })
@Html.ActionLink("EN", "", "Guests", routeValues: null, htmlAttributes: new { onclick = "SetCulture(\"en\");" })
<a href="@Url.Action("SetLanguage", "Language", new { @name = "pl" })">Polski</a>
<a href="@Url.Action("SetLanguage", "Language", new { @name = "en" })">English</a>

当然,它不起作用。我还需要什么?JavaScript函数?

最简单的答案是,您需要创建一个控制器,然后链接到该控制器

public class LanguageController : Controller
{
    public ActionResult SetLanguage(string name)
    {
        Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(name);
        Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

        HttpContext.Current.Session["culture"] = name;

        return RedirectToAction("Index", "Home");
    }
}
那么在你看来,

<a href="@Url.Action("SetLanguage", "Language", new { @name = "pl" })">Polski</a>
<a href="@Url.Action("SetLanguage", "Language", new { @name = "en" })">English</a>
编辑:

在SetLanguage操作中保存cookie:

var cookie = new HttpCookie("_culture", name);
cookie.Expires = DateTime.Today.AddYears(1);
Response.SetCookie(cookie);
在应用程序_BeginRequest中获取cookie:

var cookie = HttpContext.Current.Request.Cookies["_culture"];
var name = cookie != null ? cookie.Value : null;

我创建了一个小控制器并编辑了我的视图@Olivier(谢谢,伙计!)向我展示了一种方法,但它不起作用,因为我的应用程序将文化存储在cookie中,而不是会话中

控制器:

public class LanguageController : BaseController
    {
        // GET: Language
        public ActionResult SetLanguage(string name)
        {
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(name);
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

            HttpCookie cultureCookie = new HttpCookie("_culture");
            cultureCookie.Value = name;
            cultureCookie.Expires = DateTime.UtcNow.AddYears(1);

            Response.Cookies.Remove("_culture");
            Response.SetCookie(cultureCookie);


            return RedirectToAction("Index", "Guests");
        }
    }
LanguageController继承自BaseController(继承自Controller),因为我使用了本教程:

我认为:

@Html.ActionLink("PL", "", "Guests", routeValues: null, htmlAttributes: new { onclick = "SetCulture(\"pl\");" })
@Html.ActionLink("EN", "", "Guests", routeValues: null, htmlAttributes: new { onclick = "SetCulture(\"en\");" })
<a href="@Url.Action("SetLanguage", "Language", new { @name = "pl" })">Polski</a>
<a href="@Url.Action("SetLanguage", "Language", new { @name = "en" })">English</a>


你能提供更多关于每个用户存储区域性的信息吗?正如你所说,我创建了一个控制器并编辑了我的视图,但它在我的应用程序中不起作用。你能详细说明一下吗?有什么问题吗?这个动作被击中了吗?我的应用程序使用cookie作为区域性,而不是会话。但真的谢谢你!你给我指路了!我已经更新了帖子,只需用我刚刚发布的更新替换分配和获取变量名的行即可。确保为用户发出的每个请求设置区域性,正如我在应用程序_BeginRequest示例中所述。