how to pass parameters to asp.net web api get meth

2019-09-16 02:16发布

问题:

Following is my get method in ASP.NET Web API.

[HttpGet]
public IHttpActionResult GetDetails([FromBody] RetrieveDetails  eDetails)
{}

following is the class

public class RetrieveDetails
{ 
    public string name{ get; set; }     
    public string Type { get; set; } 
}     

When I try to call GetDetails from Fiddler eDetails is always null.

http://localhost:101222/api/emailservice/GetDetails?name=testname&Type=testtype

I tried different methods but the value is always null. If I change [HttpGet] to [HttpPost] and add the request body its working fine. But I need get method.

回答1:

Do you need [FromBody] there if you are passing values in URL for GET. You should be using [FromUri] if you are passing values in querystring.

https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api