Home > database >  MVC Attribute Routing with query parameter not working
MVC Attribute Routing with query parameter not working

Time:10-28

I am trying to combine my UI project and WebAPI project into one to make it more maintainable, however I am getting an error with the routing as below:

{
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost:64182/api/v1/business?id=101'.",
    "MessageDetail": "No type was found that matches the controller named 'api'."
}

I have added attribute routing on the method to get it to work, but it only works with the following url:

MVC ACtion:

[HttpGet, Route("api/v1/business/{id:int}")]
public IHttpActionResult Get([FromUri]int? id)
{
....
}

http://localhost:64182/api/v1/business/101

The intended url signature cannot change and it should still use the query parameter:

http://localhost:64182/api/v1/business?id=101

In the Route attribute, I cannot add a question mark because it is not allowed.

The system is already being used by many users and I cannot change the signature unfortunately otherwise this would break their systems.

How can I get this to work or what Route template can I use to include the query parameter?

CodePudding user response:

I think the attribute [FromUri] is deprecated. Try using [FromRoute]. Also, I would structure my routing from the controller class.

The following is for http://localhost:64182/api/v1/business/101

[Route("api/v1/[controller]")]
[ApiController]
public class Business : ControllerBase
{

    [HttpGet("/{id:int}")]
    public async Task<ActionResult<YourBusinessDto>> Get([FromRoute] int id)
    {
        //Your code to get your business dto here.
    }
}

The following is for http://localhost:64182/api/v1/business?id=101

[Route("api/v1/[controller]")]
[ApiController]
public class Business : ControllerBase
{

    [HttpGet]
    public async Task<ActionResult<YourBusinessDto>> Get([FromQuery] int id)
    {
        //Your code to get your business dto here.
    }
}

CodePudding user response:

In our order collection, each order has a unique identifier. We can go to the collection and request it by "id". Typical RESTful best practice, this can be retrieved by its route, for example "api/orders/1"

//api/orders/1
   [HttpGet("api/orders/{id}")]
            public string test1([FromRoute]int id)
            {
                return "test1";
            }

This attribute will instruct the ASP.NET Core framework to treat this operation as a handler for the HTTP GET verb and handle routing. We provide an endpoint template as an attribute parameter. This template is used as the route that the framework will use to match incoming requests. In this template, the {id}​​ value corresponds to the route part as the "id" parameter. The FromRoute attribute tells the framework to look up the "id" value in the route (URL) and provide it as the id parameter.

In addition, we can easily write it to use the FromQuery property. This then instructs the framework to predict a query string with an "identifier" name and corresponding integer value. Then pass the value as the id parameter to the operation. Everything else is the same.

However, the most common method is the aforementioned FromRoute usage-where the identifier is part of the URI

//api/orders?id=1
    [HttpGet("api/v1")]
            public string test2([FromQuery]int id)
            {
                return "test2";
            }

In addition, you can refer to this detailed article for more attribute usage, which may be helpful to you:

https://www.dotnetcurry.com/aspnet/1390/aspnet-core-web-api-attributes

  • Related