How to develop an ASP.NET Web API to accept a complex object as parameter?

后端 未结 1 1564
余生分开走
余生分开走 2021-01-31 14:51

I have the following Web API (GET):

public class UsersController : ApiController
{
    public IEnumerable Get(string firstName, string LastName, Dat         


        
相关标签:
1条回答
  • 2021-01-31 15:28

    By default complex types are read from body, that's why you are getting null.

    Change your action signature to

     public IEnumerable<Users> Get([FromUri]MyApiParameters parameters)
    

    if you want the model binder to pull the model from the querystring.

    You can read more about how Web API does parameter binding in the excellent article by Mike Stall from MSFT - http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx

    0 讨论(0)
提交回复
热议问题