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

时间:2018-11-23 17:05:07

标签: c# asp.net-web-api asp.net-web-api2

任务
我有一个DataMapper类,用于将数据映射到Web 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方法的角色是什么,我都需要填充它。之所以如此,是因为有时我需要条件映射以将角色特定的数据表示形式返回给客户端。

问题
我以为要做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对象在构造函数中为null。只会填入我的实际网络方法

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
    }

我是一个webapi noobie。需要指向这个问题的指针。

1 个答案:

答案 0 :(得分:2)

该请求在构造函数中尚不可用。只能在初始化控制器后才能通过操作/方法访问它。

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;     
}