Get browser language in .AspNetCore2.0?

后端 未结 4 1173
渐次进展
渐次进展 2021-02-19 08:16

I am trying to get the default language from the browser and I use the following code to get it:

var languages = HttpContext.Request.UserLanguages;
4条回答
  •  不要未来只要你来
    2021-02-19 08:54

    You need to add the localisation middleware to be able to get the IRequestCultureFeature feature:

    public void Configure(IApplicationBuilder app)
    {
        //...
    
        //Add this:
        app.UseRequestLocalization();
    
        //...
    }
    

    Now in your controller you can request the feature like this:

    var requestCulture = Request.HttpContext.Features.Get();
    

提交回复
热议问题