40

是否可以从 Uri 和 Body 绑定模型?

例如,给定以下内容:

routes.MapHttpRoute(
    name: "API Default",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

public class ProductsController : ApiController
{
    public HttpResponseMessage Put(UpdateProduct model)
    {

    }
}

public class UpdateProduct 
{
    int Id { get; set;}
    string Name { get; set; }
}

是否可以创建一个自定义活页夹,PUT以便

/api/products/1

JSON 正文为:

{
    "Name": "Product Name"
}

将导致UpdateProduct模型填充Id = 1Name = "Product Name"

更新

我知道我可以将动作签名更改为

public HttpResponseMessage Put(int id, UpdateProduct model)
{

}

但是,正如问题中所述,我特别想绑定到单个模型对象

我也将此问题发布到WebApi Codeplex 讨论论坛

4

4 回答 4

14

这是 odyth 答案的改进版本:

  1. 也适用于无形的请求,并且
  2. 除了从路由值中获取参数之外,还从查询字符串中获取参数。

为简洁起见,我只发布了 ExecuteBindingAsyncCore 方法和一个新的辅助方法,该类的其余部分是相同的。

private async Task ExecuteBindingAsyncCore(ModelMetadataProvider metadataProvider, HttpActionContext actionContext,
        HttpParameterDescriptor paramFromBody, Type type, HttpRequestMessage request, IFormatterLogger formatterLogger,
        CancellationToken cancellationToken)
{
    var model = await ReadContentAsync(request, type, Formatters, formatterLogger, cancellationToken);

    if(model == null) model = Activator.CreateInstance(type);

    var routeDataValues = actionContext.ControllerContext.RouteData.Values;
    var routeParams = routeDataValues.Except(routeDataValues.Where(v => v.Key == "controller"));
    var queryStringParams = new Dictionary<string, object>(QueryStringValues(request));
    var allUriParams = routeParams.Union(queryStringParams).ToDictionary(pair => pair.Key, pair => pair.Value);

    foreach(var key in allUriParams.Keys) {
        var prop = type.GetProperty(key, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
        if(prop == null) {
            continue;
        }
        var descriptor = TypeDescriptor.GetConverter(prop.PropertyType);
        if(descriptor.CanConvertFrom(typeof(string))) {
            prop.SetValue(model, descriptor.ConvertFromString(allUriParams[key] as string));
        }
    }

    // Set the merged model in the context
    SetValue(actionContext, model);

    if(BodyModelValidator != null) {
        BodyModelValidator.Validate(model, type, metadataProvider, actionContext, paramFromBody.ParameterName);
    }
}

private static IDictionary<string, object> QueryStringValues(HttpRequestMessage request)
{
    var queryString = string.Join(string.Empty, request.RequestUri.ToString().Split('?').Skip(1));
    var queryStringValues = System.Web.HttpUtility.ParseQueryString(queryString);
    return queryStringValues.Cast<string>().ToDictionary(x => x, x => (object)queryStringValues[x]);
}
于 2015-01-26T08:08:52.083 回答
8

您可以定义自己的 DefaultActionValueBinder。然后你可以从 body 和 uri 混合和匹配。这是一篇博客文章,其中包含用于 Web Api 的 MvcActionValueBinder 示例。使您自己的 DefaultActionValueBinderis 成为首选解决方案,因为它保证绑定器将在执行任何其他 ActionFilterAttribute 之前完成。

http://blogs.msdn.com/b/jmstall/archive/2012/04/18/mvc-style-parameter-binding-for-webapi.aspx

更新:

我在博客文章中的实现遇到了一些麻烦,并试图让它使用我的自定义媒体格式化程序。幸运的是,我所有的请求对象都是从基类扩展而来的,Request所以我制作了自己的格式化程序。

在 WebApiConfig 中

config.ParameterBindingRules.Insert(0, descriptor => descriptor.ParameterType.IsSubclassOf(typeof (Request)) ? new BodyAndUriParameterBinding(descriptor) : null);

BodyAndUriParameterBinding.cs

public class BodyAndUriParameterBinding : HttpParameterBinding
{
    private IEnumerable<MediaTypeFormatter> Formatters { get; set; }
    private IBodyModelValidator BodyModelValidator { get; set; }
    public BodyAndUriParameterBinding(HttpParameterDescriptor descriptor)
        : base (descriptor)
    {
        var httpConfiguration = descriptor.Configuration;
        Formatters = httpConfiguration.Formatters;
        BodyModelValidator = httpConfiguration.Services.GetBodyModelValidator();
    }

    private Task<object> ReadContentAsync(HttpRequestMessage request, Type type,
        IEnumerable<MediaTypeFormatter> formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)
    {
        var content = request.Content;
        if (content == null)
        {
            var defaultValue = MediaTypeFormatter.GetDefaultValueForType(type);
            return defaultValue == null ? Task.FromResult<object>(null) : Task.FromResult(defaultValue);
        }

        return content.ReadAsAsync(type, formatters, formatterLogger, cancellationToken);
    }

    public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext,
        CancellationToken cancellationToken)
    {
        var paramFromBody = Descriptor;
        var type = paramFromBody.ParameterType;
        var request = actionContext.ControllerContext.Request;
        var formatterLogger = new ModelStateFormatterLogger(actionContext.ModelState, paramFromBody.ParameterName);
        return ExecuteBindingAsyncCore(metadataProvider, actionContext, paramFromBody, type, request, formatterLogger, cancellationToken);
    }

    // Perf-sensitive - keeping the async method as small as possible
    private async Task ExecuteBindingAsyncCore(ModelMetadataProvider metadataProvider, HttpActionContext actionContext,
        HttpParameterDescriptor paramFromBody, Type type, HttpRequestMessage request, IFormatterLogger formatterLogger,
        CancellationToken cancellationToken)
    {
        var model = await ReadContentAsync(request, type, Formatters, formatterLogger, cancellationToken);

        if (model != null)
        {
            var routeParams = actionContext.ControllerContext.RouteData.Values;
            foreach (var key in routeParams.Keys.Where(k => k != "controller"))
            {
                var prop = type.GetProperty(key, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
                if (prop == null)
                {
                    continue;
                }
                var descriptor = TypeDescriptor.GetConverter(prop.PropertyType);
                if (descriptor.CanConvertFrom(typeof(string)))
                {
                    prop.SetValue(model, descriptor.ConvertFromString(routeParams[key] as string));
                }
            }
        }

        // Set the merged model in the context
        SetValue(actionContext, model);

        if (BodyModelValidator != null)
        {
            BodyModelValidator.Validate(model, type, metadataProvider, actionContext, paramFromBody.ParameterName);
        }
    }
}

请求.cs

public abstract class Request : IValidatableObject
{
    public virtual IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        yield return ValidationResult.Success;
    }
}
于 2014-07-20T02:36:02.077 回答
5

好吧,我想出了一个办法。基本上,我做了一个动作过滤器,它将在模型从 JSON 填充后运行。然后它将查看 URL 参数,并在模型上设置适当的属性。完整来源如下:

using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;


public class UrlPopulatorFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        var model = actionContext.ActionArguments.Values.FirstOrDefault();
        if (model == null) return;
        var modelType = model.GetType();
        var routeParams = actionContext.ControllerContext.RouteData.Values;

        foreach (var key in routeParams.Keys.Where(k => k != "controller"))
        {
            var prop = modelType.GetProperty(key, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
            if (prop != null)
            {
                var descriptor = TypeDescriptor.GetConverter(prop.PropertyType);
                if (descriptor.CanConvertFrom(typeof(string)))
                {
                    prop.SetValueFast(model, descriptor.ConvertFromString(routeParams[key] as string));
                }
            }
        }
    }
}
于 2013-09-20T20:11:31.777 回答
0

如果我理解你,这应该是开箱即用的,例如这对我有用:

    [HttpPost]
    public ActionResult Test(TempModel model)
    {
        ViewBag.Message = "Test: " + model.Id +", " + model.Name;

        return View("About");
    }

public class TempModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

并根据请求:localhost:56329/Home/Test/22 with body:{"Name":"tool"}

我将模型的属性设置为 22 和“工具”。

于 2013-07-15T02:34:43.027 回答