我的印象是,ASP.Net Web API中的模型绑定应该支持与MVC支持的最低级别的功能绑定。ASP.Net Web API模型绑定在MVC中不起作用3
采取以下控制器:
public class WordsController : ApiController
{
private string[] _words = new [] { "apple", "ball", "cat", "dog" };
public IEnumerable<string> Get(SearchModel searchSearchModel)
{
return _words
.Where(w => w.Contains(searchSearchModel.Search))
.Take(searchSearchModel.Max);
}
}
public class SearchModel
{
public string Search { get; set; }
public int Max { get; set; }
}
我与请求它:
http://localhost:62855/api/words?search=a&max=2
不幸的是,模型不绑定,因为它会在MVC。为什么这不像我所期望的那样具有约束力?我将在我的应用程序中有很多不同的模型类型。如果绑定正常工作就好了,就像在MVC中一样。
也许帮助你,这[文章] [1]的问题。 [1]:http://stackoverflow.com/questions/12072277/reading-fromuri-and-frombody-at-the-same-time – Cagdas