-1

In my asp.net core application, we are using the default MVC routing in App StartUp configuration.

Then in our controller, we have the following code for one of our api endpoint method:

  [HttpGet]
  [Route("services/task/{taskid?}/{taskmessage?}")]
  [Produces("text/html")]
  public async Task<ActionResult> TaskMessage(string taskId = null, string queue = null, string taskmessage = null)

Then we are trying to call the endpoint by passing taskmessage only as the following URL:
http://localhost:12345/api/services/task?taskmessage=hello

However, it's not working here. Can anyone please let me know where did I got wrong? It was working in our ASP.Net project, but once we migrated to the ASP.NET core, it's not working.

BTW, the version of ASP.NET core F/W we are currently using is ASP.NET Core 2.1, if it matters.

5
  • 1
    What does not working mean? Are you getting any errors? Commented Dec 3, 2019 at 2:10
  • Hi @MattU, sorry I didn't make it clear. Not working here means in the URL I passed a value "hello" to the parameter "taskmessage", but in the method parameter, the "taskmessage" is still null. This is our issue. Commented Dec 3, 2019 at 2:14
  • I've not tried this in .Net Core. Instead, I use [FromQuery] for query string parameters. Like this: ([FromQuery] string taskMessage). You can use it for multiple parameters. And the template in the Route attribute is simpler. Commented Dec 3, 2019 at 2:46
  • Thanks @MattU I know we can use [FromQuery] for the query string parameters. But, from design perspective, I would like to know why is not bind in .Net Core. Why it's different with .Net... But, thank you anyway, I just tried with [FromQuery] and it's working for me Commented Dec 3, 2019 at 3:00
  • Also, it's probably better to use the HttpGet attribute and supply the route template there. [HttpGet("route/template")]. The [Route] attribute means the action will accept all HTTP verbs. learn.microsoft.com/en-us/aspnet/core/mvc/controllers/… ... See the green tip in that section. Commented Dec 3, 2019 at 3:01

1 Answer 1

0

As this answer to a similar (but different) question states: Can't bind params using [FromQuery] ASP Net Core 2 API

[FromHeader], [FromQuery], [FromRoute], [FromForm]: Use these to specify the exact binding source you want to apply.

They referenced this link: https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.1 which talks about the various model binding methods.

Sign up to request clarification or add additional context in comments.

Comments

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.