标签:
突然想做一个多语言网站,确不知道怎么实现好,突然想到了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]); } }
标签:
原文地址:http://www.cnblogs.com/dyg540/p/5551560.html