zoukankan      html  css  js  c++  java
  • HttpCookie加匿名类实现多语言

    突然想做一个多语言网站,确不知道怎么实现好,突然想到了HttpCookie,然后页面后台用匿名类实现语言的储存。

                string lan = Request["str_lan"];
                ViewBag.lan = lan;
    
                HttpCookie hc = new HttpCookie("language");
                hc.Value = lan;
                hc.Expires = DateTime.Now.AddDays(1);
                Response.Cookies.Add(hc);        

    HttpCookie用来当前记录语言种类。

    前台页面取值:

    @{
        ViewBag.Title = "Home Page";
        var language = new Dictionary<string, object>();
        language.Add("zh_cn", new { name = "邓永高" });
        language.Add("en", new { name = "dyg" });
    
        string get_lan = string.Empty;
        HttpCookie cookie_language = Request.Cookies["language"];
        if (cookie_language != null)
        {
            get_lan = cookie_language.Value;
        }
    
    }
    
    
    @using MVCMutiLan.Controllers;
    
    <h2>@Html.getPageLanVal(language, get_lan, "name").ToString()</h2>
    <form action="/Home/Index" method="post">
    
        <select name="str_lan">
            <option value="zh_cn">简体中文</option>
            <option value="en">英文</option>
        </select>  <input type="submit" value="设置语言" />
    
    </form>
    
    @ViewBag.lan

    控件取值的扩展方法:

     public static class HtmlHelpers
        {
            public static object getPageLanVal(this HtmlHelper helper, Dictionary<string, object> dic, string strlan, string Lanvalue)
            {
                return dic[strlan].GetType().GetProperties().Where(x => x.Name == Lanvalue).FirstOrDefault().GetValue(dic[strlan]);
            }
        }
  • 相关阅读:
    stl(8)常见的算法
    stl(7)几种常见的迭代器
    stl(6)deque容器
    pre_exam_exercise1
    全为1时计算个数,出现0返回0
    COMP9021--7.18
    lecture 5 Basics of Map Algebra
    COMP9021--7.15
    Lecture 4补充
    COMP9021--7.4
  • 原文地址:https://www.cnblogs.com/dyg540/p/5551560.html
Copyright © 2011-2022 走看看