English 中文(简体)
MVC http://module
原标题:MVC Localication set culture in httpmodule

我已在我的MVC3申请中实施当地化。

i 寻求在“行动基金”执行之前建立当前文化的解决办法。

i) 欲从乌勒获得目前的文化:

routes.MapRoute(
                "Language",
                "{culture}/{controller}/{action}/{id}",
                new { culture = "en", controller = "Page", action = "Index", id = UrlParameter.Optional }
                );

在基地控制器中,可以做到:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var culture = filterContext.RouteData.Values["culture"] ?? ConfigurationSettings.AppSettings["DefaultCulture"];
            var cultureInfo = CultureInfo.GetCultureInfo((string)culture);
            Thread.CurrentThread.CurrentCulture = cultureInfo;
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
            base.OnActionExecuting(filterContext);
        }

i 希望在http://module上更新申请,并确立目前文化。

at the moment my code looks like this:

public void Init(HttpApplication httpApplication)
        {
            httpApplication.BeginRequest += (sender, eventArgs) =>
            {
                var defaultCulture = ConfigurationSettings.AppSettings["DefaultCulture"];
                CultureInfo cultureInfo = CultureInfo.GetCultureInfo(defaultCulture);
                try
                {
                    Thread.CurrentThread.CurrentCulture = cultureInfo;
                    Thread.CurrentThread.CurrentUICulture = cultureInfo;
                }
                catch { }
            };
        }

• 如何利用过滤文化建立现有文化。 LineData.Values [“文化”] in the httpmodule ?

感谢您提供的任何帮助

Ori

问题回答

You should use a custom route handler to set the culture accordingly to the route information. See example below:

public class CultureMvcRouteHandler : MvcRouteHandler
{
    protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        if (requestContext == null)
        {
            throw new ArgumentNullException("requestContext");
        }

        string culture = requestContext.RouteData.Values["culture"] as string ?? "";
        CultureInfo ci = CreateCultureInfo(culture);
        Thread.CurrentThread.CurrentUICulture = ci;

        return base.GetHttpHandler(requestContext);
    }

    private CultureInfo CreateCultureInfo(string culture)
    {
        if (culture == null)
        {
            throw new ArgumentNullException("culture");
        }

        CultureInfo ci = null;

        try
        {
            ci = new CultureInfo(culture);
        }
        catch (CultureNotFoundException)
        {
            ci = CultureInfo.InvariantCulture;
        }

        return ci;
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签