Reputation: 337
I am using .NET 4.5 framework to build a REST API. This API has a get
method with multiple parameters, but when I call the API, then it shows the bad request second parameter is null. In reality, I am passing the value for second parameter. Below is the method implementation,
[HttpGet()]
[Route("api/Mycontroller/{useid:int}")]
public List<GetDetails> GET([FromUri()] int useid, DateTime loginDate)
{
// Some logic
}
Request through C# code:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost/abc/");
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage clientResponse = Task.Run(async () => { return await client.GetAsync("api/Mycontroller/" + userid+"?LoginDate=" +TodaysDateParameter); }).Result;
Here result is returning null value due to bad request.
I tried various possibilities of query string, but nothing worked. I tried to remove route fromuri()
, fromBody()
but that was also not helpful.
What am I doing wrong?
Upvotes: 0
Views: 3228
Reputation: 754230
You need to also add your second parameter to the [Route]
attribute on your method - like this:
[Route("api/Mycontroller/{useid:int}/{loginDate}")]
public List<GetDetails> GET([FromUri()] int useid, DateTime loginDate)
Then you can call your API method like this:
string queryUrl = $"api/Mycontroller/{userid}/{TodaysDateParameter}";
HttpResponseMessage clientResponse = Task.Run(async () => { return await client.GetAsync(queryUrl).Result;
Upvotes: 1