WebApi - 从Uri和Body中绑定

kim*_*gro 38 c# asp.net-web-api

是否可以绑定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; }
}
Run Code Online (Sandbox Code Playgroud)

是否有可能创建一个定制绑定,这样一PUT

/ API /产品/ 1

使用JSON正文:

{
    "Name": "Product Name"
}
Run Code Online (Sandbox Code Playgroud)

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

更新

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

public HttpResponseMessage Put(int id, UpdateProduct model)
{

}
Run Code Online (Sandbox Code Playgroud)

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

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

Kon*_*man 13

这是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]);
}
Run Code Online (Sandbox Code Playgroud)

  • 这看起来很棒.这段代码是否完成了默认的'FromBody'*和*'FromUri'属性组合的所有内容?另外,你知道ASP.NET团队决定不包含"FromBodyAndUri"开箱即用的东西的原因是什么?我经历了无数博客文章,但无法真正找到为什么这会是不好的做法......我的所有控制器都接受了`IRequest <>`的实现([Jimmy Bogard的Mediator模式](https://github.com/jbogard/MediatR)),包含一个对象中的所有参数.能够用身体和uri参数填充它会很棒. (3认同)

ody*_*yth 7

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

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

更新:

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

在WebApiConfig中

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

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*ies 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));
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您有其他依赖于完全绑定模型的ActionFilterAttributes,则此解决方案不起作用,因为ActionFilterAttributes没有保证的操作顺序. (2认同)

Fel*_*lix 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 }
        );
Run Code Online (Sandbox Code Playgroud)

并根据请求: localhost:56329/Home/Test/22 ,正文:{"Name":"tool"}

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