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?

6
  • @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. Commented Apr 28, 2019 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. Commented Apr 28, 2019 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 Commented Apr 28, 2019 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. Commented Apr 28, 2019 at 15:42
  • its datetime in c# code . Datetime format is DD-MM-YYYY HH:MM:SS Commented Apr 29, 2019 at 6:27

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

DateTime parameters have their own problems though: stackoverflow.com/questions/14359566/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.