0

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Pavan
  • 337
  • 5
  • 23
  • @CamiloTerevinto if you can read the complete question then you will understand the question and various implementation that i had tried.I will be grate full if you help to solve problem rather than negative vote and comment. – Pavan Apr 28 '19 at 12:37
  • Why the downvote, the question is clear enough? First thing to try is to define the `loginDate` parameter as a string. This way the model binding won't need to attempt to convert the parameter to a DateTime and you'll see that it is indeed possible to pass a second parameter. You haven't indicated the format of your `TodaysDateParameter`, but I suspect it's a format that's not recognized by the model binder. – Joe Apr 28 '19 at 12:46
  • @Joe: When i Define the parametertype for todaysDateParameter in route like TodaysDateParameter:Datatime} That is also not helpful. The strange thing is that datetime parameter passed to controller is reaching in a same way. What i mean is I have MVC controller which get the parameters from AJAX call, this controller create httpclient request and internally call the webapi.Controller is getting the value but not api – Pavan Apr 28 '19 at 12:53
  • You still haven't said what is `TodaysDateParameter` in your C# code that makes the request. Is it a `DateTime`? In which case your code will format it as a string using the default, culture-specific format. Which is probably not what your webapi is expecting. – Joe Apr 28 '19 at 15:42
  • its datetime in c# code . Datetime format is DD-MM-YYYY HH:MM:SS – Pavan Apr 29 '19 at 06:27
  • I would have thought the default model binder would expect a data format without spaces, probably ISO8601. Try yyyyMMddTHHmmss or yyyy-MM-ddTHH-mm-ss, – Joe Apr 29 '19 at 09:57

1 Answers1

1

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;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    DateTime parameters have their own problems though: https://stackoverflow.com/questions/14359566/how-to-pass-a-datetime-parameter – CodeCaster Apr 28 '19 at 13:27