ASP.NET MVC 多语言配置
来源:互联网 发布:淘宝运费模板删不了 编辑:程序博客网 时间:2024/05/16 05:01
步骤1:打开VS2015新建测试项目。
步骤2:创建资源文件 App_GlobalResources下。
namespace WebApplication11.App_Code
{
publicclassCultureModule : IHttpModule
{
privateCultureInfo currentCulture;
privateCultureInfo currentUICulture;
publicvoid Dispose() { }
publicvoid Init(HttpApplication context)
{
context.BeginRequest += SetCurrentCulture;
context.EndRequest += RecoverCulture;
}
privatevoid SetCurrentCulture(object sender, EventArgs args)
{
currentCulture =Thread.CurrentThread.CurrentCulture;
currentUICulture =Thread.CurrentThread.CurrentUICulture;
HttpContextBase contextWrapper = newHttpContextWrapper(HttpContext.Current);
RouteData routeData = RouteTable.Routes.GetRouteData(contextWrapper);
if (routeData == null)
{
return;
}
object culture;
if (routeData.Values.TryGetValue("lang",out culture))
{
try
{
Thread.CurrentThread.CurrentCulture = newCultureInfo(culture.ToString());
Thread.CurrentThread.CurrentUICulture = newCultureInfo(culture.ToString());
}
catch
{ }
}
}
privatevoid RecoverCulture(object sender, EventArgs args)
{
Thread.CurrentThread.CurrentCulture = currentCulture;
Thread.CurrentThread.CurrentUICulture = currentUICulture;
}
}
}
publicstaticvoid RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//以下是新加的路由配置,Begin-----------------------------------------------------------------
routes.MapRoute(
"Globalization",// 路由名称
"{lang}/{controller}/{action}/{id}",// 带有参数的 URL
new { lang = "zh", controller = "Home", action = "Index", id = UrlParameter.Optional }, // 参数默认值
new { lang = "^[a-zA-Z]{2}(-[a-zA-Z]{2})?$" } //参数约束
);
//-------------------End------------------------------------------------
routes.MapRoute(
name:"Default",
url:"{controller}/{action}/{id}",
defaults:new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
步骤5:配置webconfig
<system.webServer>
<modules>
<addname="CultureModule"type="WebApplication11.App_Code.CultureModule"/>
<removename="FormsAuthentication" />
</modules>
</system.webServer>
步骤6:View里面直接调用
@Resources.Resource1.String1
步骤7:直接运行,搞定!
http://localhost:6068/zh-cn
http://localhost:6068
- ASP.NET MVC 多语言配置
- asp.net mvc配置
- ASP.NET MVC 多语言解决方案
- ASP.NET MVC 多语言方案
- ASP.NET MVC 多语言方案
- ASP.NET MVC 多语言技术
- ASP.NET MVC路由配置
- ASP.NET MVC路由配置
- ASP.NET MVC 配置说明
- Asp.Net MVC 路由配置
- ASP.NET MVC资源文件多语言实现方式
- ASP.NET MVC多语言 仿微软网站效果
- Spring.net之Asp.net mvc配置
- asp.net mvc 环境配置问题
- ASP.Net MVC框架配置与分析
- 在IIS6中配置asp.net MVC
- win7配置iis支持asp.net mvc
- ASP.NET MVC网站路由配置
- 韦东山u-boot、kernel解压缩、打补丁、配置、编译、烧写操作试验
- AngularJS开发指南28:创建服务
- 9、Power Map—应用拾取坐标系统确定经纬度
- AngularJS $http跨域
- Android 获取音视频文件的播放时长
- ASP.NET MVC 多语言配置
- centos 7禁用ipv6
- 原生JavaScript实现的alert弹框
- ZOJ2868【折半】
- Iphone手机页面消除数字、邮箱的颜色
- Leetcode 486. Predict the Winner 预测赢家 解题报告
- 轮播图bunner控件ConvenientBanner
- AngularJS $http服务例子
- Spring—AOP的ThrowsAdvice接口问题