在控制器asp.net-core中获取当前区域性 [英] Get the current culture in a controller asp.net-core

查看:166
本文介绍了在控制器asp.net-core中获取当前区域性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的视图设置了区域性,并在控制器中更改了区域性,但是我似乎找不到如何知道控制器中当前正在使用的区域性,我正在寻找类似的东西:

I have setup the cultures for my views and changing the culture in a controller but I can't seem to find how to know what culture I'm currently using in a controller, I'm looking for something like:

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

推荐答案

答案在请求对象上,这里是代码:

The answer was on the Request Object, here's 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();
}

这篇关于在控制器asp.net-core中获取当前区域性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆