mvc 中英文切换

来源:互联网 发布:成都神鸟数据怎么样 编辑:程序博客网 时间:2024/06/05 15:14

我常用的2个方案,其实性质是一样的

方案1,使用过滤器

ActionFilterAttribute,这个就不细说了,比较方便。

实现一个继承自ActionFilterAttribute的类,实现OnActionExecuting方法。就可以了。


方案2,使用IHttpModule

这个给一下代码,

1 实现IHttpModule

public class MyHttpModule : IHttpModule    {        public void Init(HttpApplication context)        {            context.BeginRequest += new EventHandler(context_BeginRequest);        }        private CultureInfo currentCulture;        void context_BeginRequest(object sender, EventArgs e)        {            HttpApplication httpApp = (HttpApplication)sender;            currentCulture = Thread.CurrentThread.CurrentUICulture;            TestMVC.Models.ModelTest.title = "嗨";            string s = "统一处理所有文件请求";        }        public void Dispose()        {            throw new NotImplementedException();        }    }


2  中英文类(这里就创建一个 英文的,中文的直接在BeginRequest中赋值中文了)

    public class ModelTest    {        public static string title = "hello";        public static string content = "how do you do";                }


3  注册web.config

<system.webServer>    <modules>      <add name="MyHttpModule" type="HttpModule.MyHttpModule"/>    </modules>  </system.webServer>


效果:

查询到了浏览器语言版本,zh-CN



前台显示ModelTest.title