标签:style blog http color io os 使用 ar 文件
MyModule类的具体实现:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Routing;
namespace MVC4多语言IHttpModule实现.Lang
{
public class MyModule:IHttpModule
{
private CultureInfo currentCulture;
private CultureInfo currentUICulture;
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += SetCurrentCulture;
context.EndRequest += RecoverCulture;
}
private void SetCurrentCulture(object sender, EventArgs args)
{
currentCulture = Thread.CurrentThread.CurrentCulture;
currentUICulture = Thread.CurrentThread.CurrentUICulture;
HttpContextBase contextWrapper = new HttpContextWrapper(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 = new CultureInfo(culture.ToString());
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture.ToString());
}
catch
{ }
}
}
private void RecoverCulture(object sender, EventArgs args)
{
Thread.CurrentThread.CurrentCulture = currentCulture;
Thread.CurrentThread.CurrentUICulture = currentUICulture;
}
}
}
然后添加路由 规则:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace MVC4多语言IHttpModule实现
{
// 注意: 如需啟用 IIS6 或 IIS7 傳統模式的說明,
// 請造訪 http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);//在此方法中添加路由规则
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace MVC4多语言IHttpModule实现
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
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})?$" } //参数约束
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
标签:style blog http color io os 使用 ar 文件
原文地址:http://www.cnblogs.com/unintersky/p/3969612.html