查询字符串模型绑定ASP

查询字符串模型绑定ASP

本文介绍了查询字符串模型绑定ASP.NET WebApi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下型号

public class Dog
{
    public string NickName { get; set; }
    public int Color { get; set; }
}

并且我有以下通过API公开的api控制器方法

and I have the following api controller method which is exposed through an API

public class DogController : ApiController
{
  // GET /v1/dogs
  public IEnumerable<string> Get([FromUri] Dog dog)
  { ...}

现在,我要发出GET请求,如下所示:

Now, I would like to issue the GET request as follows:

GET http://localhost:90000/v1/dogs?nick_name=Fido&color=1

问题:如何将查询字符串参数nick_name绑定到dog类中的属性NickName?我知道我可以在不带下划线的情况下调用API(即昵称),也可以将NickName更改为Nick_Name并获取值,但是为了惯例起见,我需要保留这些名称.

Question: How do I bind the query string parameter nick_name to property NickName in the dog class? I know I can call the API without the underscore (i.e. nickname) or change NickName to Nick_Name and get the value, but I need the names to remain like that for convention.

编辑这个问题不是重复的,因为它是关于ASP.NET WebApi而不是ASP.NET MVC 2

EditThis question is not a duplicate because it is about ASP.NET WebApi not ASP.NET MVC 2

推荐答案

实现IModelBinder

public class DogModelBinder : IModelBinder
{
    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
    {
        if (bindingContext.ModelType != typeof(Dog))
        {
            return false;
        }

        var model = (Dog)bindingContext.Model ?? new Dog();


        var hasPrefix = bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName);

        var searchPrefix = (hasPrefix) ? bindingContext.ModelName + "." : "";

        model.NickName = GetValue(bindingContext, searchPrefix, "nick_name");

        int colorId = 0;
        if (int.TryParse(GetValue(bindingContext, searchPrefix, "colour"), out colorId))
        {
            model.Color = colorId; // <1>
        }

        bindingContext.Model = model;

        return true;
    }

    private string GetValue(ModelBindingContext context, string prefix, string key)
    {
        var result = context.ValueProvider.GetValue(prefix + key); // <4>
        return result == null ? null : result.AttemptedValue;
    }
}

然后创建ModelBinderProvider

public class DogModelBinderProvider : ModelBinderProvider
{
    private CollectionModelBinderProvider originalProvider = null;

    public DogModelBinderProvider(CollectionModelBinderProvider originalProvider)
    {
        this.originalProvider = originalProvider;
    }

    public override IModelBinder GetBinder(HttpConfiguration configuration, Type modelType)
    {
        // get the default implementation of provider for handling collections
        IModelBinder originalBinder = originalProvider.GetBinder(configuration, modelType);

        if (originalBinder != null)
        {
            return new DogModelBinder();
        }

        return null;
    }
}

并在控制器中使用类似

public IEnumerable<string> Get([ModelBinder(typeof(DogModelBinder))] Dog dog)
{
    //controller logic
}

这篇关于查询字符串模型绑定ASP.NET WebApi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 18:26