Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC选定语言_C#_Asp.net Mvc 4_Cookies - Fatal编程技术网

C# ASP.NET MVC选定语言

C# ASP.NET MVC选定语言,c#,asp.net-mvc-4,cookies,C#,Asp.net Mvc 4,Cookies,我正在制作简单的多语言网站。让我在母版页上选择语言,使用HttpCookie cookies=HttpContext.Request.cookies[“语言”]正常吗以了解当前选择的语言?还是存在另一种更好的方法 家庭控制器: public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { HttpCookie cookies =

我正在制作简单的多语言网站。让我在母版页上选择语言,使用
HttpCookie cookies=HttpContext.Request.cookies[“语言”]正常吗以了解当前选择的语言?还是存在另一种更好的方法

家庭控制器:

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        HttpCookie cookies = HttpContext.Request.Cookies["Language"];
        return View();
    }

}
public class LanguageController : Controller
{
    //
    // GET: /Language/

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Change(string abbr)
    {
        if (abbr!=null)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(abbr);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(abbr);
        }
        HttpCookie cookies = new HttpCookie("Language");
        cookies.Value = abbr;
        Response.Cookies.Add(cookies);
        return RedirectToAction("Index","Home");
    }
}
语言控制器:

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        HttpCookie cookies = HttpContext.Request.Cookies["Language"];
        return View();
    }

}
public class LanguageController : Controller
{
    //
    // GET: /Language/

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Change(string abbr)
    {
        if (abbr!=null)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(abbr);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(abbr);
        }
        HttpCookie cookies = new HttpCookie("Language");
        cookies.Value = abbr;
        Response.Cookies.Add(cookies);
        return RedirectToAction("Index","Home");
    }
}
_布局:

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    @Html.ActionLink(MultiLanguages.Resources.HomeText.About, "Home")
    @Html.ActionLink("France", "Change", "Language", new { abbr = "fr" }, null)
    @Html.ActionLink("English", "Change", "Language", new { abbr = "en" }, null)
    @RenderBody()
    @Scripts.Render("~/bundles/jquery")
    @RenderSection("scripts", required: false)
</body>
</html>