Asp.net mvc 5 web api 2-将数据作为参数从操作筛选器传递到操作

Asp.net mvc 5 web api 2-将数据作为参数从操作筛选器传递到操作,asp.net-mvc-5,asp.net-web-api2,Asp.net Mvc 5,Asp.net Web Api2,为了避免获取每个操作的用户数据,我创建了一个自定义操作过滤器,它通过用户ID获取用户,然后传递给操作 public class UserDataAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { ... // getting the user

为了避免获取每个操作的用户数据,我创建了一个自定义操作过滤器,它通过用户ID获取用户,然后传递给操作

public class UserDataAttribute : ActionFilterAttribute
{    
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            ...
            // getting the user and storing it in the request properties
            object user = userBLL.GetUserById(userId);
            actionContext.Request.Properties.Add("User", user);
        }
}
我可以在action方法中获取用户对象,如下所示:

[Authorize]
[UserData]
[HttpGet]
[Route("dosomething")]
public IHttpActionResult DoSomething()
{
      // retrieve the user
      object user;
      Request.Properties.TryGetValue("User", out user);
      User u = (User)user;

      return Ok();
}
但是,在MVC中,可以在过滤器中使用ActionParameters来存储action方法将使用的内容,如下所示:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
     ...

     // Create object parameter.
     filterContext.ActionParameters["User"] = userBLL.GetUserById(userId);
 }
然后将用户对象当作原始请求的一部分使用:

[AddActionParameter]
public ActionResult Index(User user)
{
    // Here I can access the user setted on the filter
    ...

    return View();
}

所以,我的问题是:在WebAPI2中有一种方法可以将用户对象作为参数从动作过滤器传递到动作,就像在MVC中一样

使用ASP.NET Web API,您可以创建一个参数绑定来接收对象,即您案例中的用户。您不必为此创建筛选器。因此,您将创建这样的绑定

public class UserParameterBinding : HttpParameterBinding
{
    public UserParameterBinding(HttpParameterDescriptor descriptor) : 
                                                        base(descriptor) { }

    public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, 
                                               HttpActionContext context,
                                                  CancellationToken cancellationToken)
    {
        SetValue(context, new User() { // set properties here });

        return Task.FromResult<object>(null);
    }
}

这样,无论您在哪里使用User作为操作方法参数,它都会自动将您在UserParameterBinding中创建的实例绑定到该参数。

使用ASP.NET Web API,您可以创建一个参数绑定来接收一个对象,即您案例中的用户。您不必为此创建筛选器。因此,您将创建这样的绑定

public class UserParameterBinding : HttpParameterBinding
{
    public UserParameterBinding(HttpParameterDescriptor descriptor) : 
                                                        base(descriptor) { }

    public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, 
                                               HttpActionContext context,
                                                  CancellationToken cancellationToken)
    {
        SetValue(context, new User() { // set properties here });

        return Task.FromResult<object>(null);
    }
}

这样,无论您在哪里使用User作为操作方法参数,它都会自动将您在UserParameterBinding中创建的实例绑定到该参数。

太好了,非常感谢!记录如下:为了在ExecuteBindingAsync方法中注入“userBLL”,我使用了_userBLL=iuserball context.ControllerContext.Configuration.DependencyResolver.GetServicetypeofIUserBLL;太好了,非常感谢!记录如下:为了在ExecuteBindingAsync方法中注入“userBLL”,我使用了_userBLL=iuserball context.ControllerContext.Configuration.DependencyResolver.GetServicetypeofIUserBLL;