Get current culture in asp.net-core controller

I set up cultures for my views and changed the culture in the controller, but I can’t find how to find out which culture I use in the controller, I am looking for something like:

public class HomeController : Controller {
  public async Task<IActionResult> Index()
  {
      // Something like the next line
      var requestCulture = GetRequestedCulture()
      return View();
  }
}

thank you for your help

+4
source share
2 answers

The response was on the Request Object, here is the code:

public async Task<IActionResult> Index() {
    // Retrieves the requested culture
    var rqf = Request.HttpContext.Features.Get<IRequestCultureFeature>();
    // Culture contains the information of the requested culture
    var culture = rqf.RequestCulture.Culture;
    return View();
}
+9
source

JohnnysAce answer works. If you just need an easy way to get the current culture, this is done as always in .net:

CultureInfo uiCultureInfo = Thread.CurrentThread.CurrentUICulture;
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;

IRequestCultureFeature (. JohnnyAces, - ), Startup.cs. Microsoft https://github.com/aspnet/Entropy/blob/dev/samples/Localization.StarterWeb/Startup.cs

0

Source: https://habr.com/ru/post/1664598/


All Articles