我应该如何在 ApiController 构造函数中获取请求?

How should I get the Request in ApiController constructor?

任务
我有一个 DataMapper class,用于将数据映射到我的网络 api 的移动客户端的自定义表示中。

public class DataMapper
    {
        public static string Role { get; set; }
        public static RoleReturnModel Create(IdentityRole appRole)
        {
            return new RoleReturnModel
            {
                Id = appRole.Id,
                Name = appRole.Name
            };

        }
        public static CountryReturnModel Create(Country country)
        {
            return new CountryReturnModel
            {
                Id = country.Id,
                Name = country.Name,
                CityList = country.Cities.Select(city => DataMapper.Create(city))
            };

        }
        public static CityReturnModel Create(City city)
        {
            return new CityReturnModel
            {
                Id = city.Id,
                Name = city.Name,
            };

        }
}

如您所见,第一个 属性 称为角色。我需要用访问我的 web 方法的任何角色来填充它。之所以如此,是因为有时我需要条件映射到 return 到客户端的角色特定数据表示。

问题
我认为 DataMapper.Role = CurrentRole 的最佳位置是在我的 ApiController

的构造函数中
public class BaseApiController : ApiController
{
    private ModelFactory _modelFactory;
    private ApplicationUserManager _AppUserManager = null;
    private ApplicationRoleManager _AppRoleManager = null;

    protected BaseApiController()
    {
       //Request is null here
        DataMapper.Role = Request.GetOwinContext().GetUserManager<ApplicationRoleManager>().FindById(User.Identity.GetUserId()).Name; 
    }

但这不起作用。 Request 对象在构造函数中为空。它只在我实际的网络方法中得到填充

public class UsersController : BaseApiController
{

    IUserRepository UserRepository;
    public UsersController() // will use ninject for constructor injection
    {
        UserRepository = new UserRepository();
    }

    [Route("profile")]
    public IHttpActionResult GetUser()
    {
         //Request is available here
    }

我是网络api新手。需要指向这个问题。

该请求在构造函数中尚不可用。控制器已经初始化后,您只能在 action/method 中访问它。

public class BaseApiController : ApiController {
    private ModelFactory _modelFactory;
    private ApplicationUserManager _AppUserManager = null;
    private ApplicationRoleManager _AppRoleManager = null;

    protected string GetRole() {
        return Request.GetOwinContext()
            .GetUserManager<ApplicationRoleManager>()
            .FindById(User.Identity.GetUserId()).Name;     
    }

并访问

public class UsersController : BaseApiController {

    IUserRepository UserRepository;
    public UsersController() // will use ninject for constructor injection
    {
        UserRepository = new UserRepository();
    }

    [Route("profile")]
    public IHttpActionResult GetUser()
    {
         //Request is available here
        var role = GetRole();
    }

或者考虑将其提取到扩展方法中以便重复使用

var role = this.GetUserRole();

哪里

public static string GetUserRole(this ApiController controller) {
    var request = controller.Request;
    var user = controller.User
    return request.GetOwinContext()
        .GetUserManager<ApplicationRoleManager>()
        .FindById(user.Identity.GetUserId()).Name;     
}