Create route to webapi method with parameters via querystring

Asked

Viewed 832 times

5

I have the following route in class WebApiConfig of a project Aspnet Wepapi

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{controller}/{action}/{id}"
        );

Edited: Using @Gabrielcoletta’s reply: The Route was the way it is below, and now the url works http://localhost:62027/Pedidos/Todos?pagina=1&filtro=117 but does not work the following url http://localhost:62027/Pedidos/Todos?filtro=117, only works when both parameters are passed in querystring.

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

What works properly for the URL and methods below:

[HttpGet]
[ResponseType(typeof(Pedido))]
public async Task<IHttpActionResult> Detalhes(int id)

http://localhost:62027/Pedidos/Detalhes/104

But for the URL and method below does not work

[HttpGet]
[ResponseType(typeof(IPagedList<Pedido>))]
public async Task<IHttpActionResult> Todos(string filtro, string pagina)

http://localhost:62027/Pedidos/Todos?filtro=s&pagina=1
http://localhost:62027/Pedidos/Todos

Being displayed the following message:

inserir a descrição da imagem aqui

I have another project Aspnet MVC which has the same route and works properly, why does the Webapi not work? which is the correct way?

  • Webapi project has only one route and a single route file

2 answers

4


It is not finding because the {id} of its route is not optional, so all its route has to have a value for {id} or it will not find route.

Set your route like this and it will work:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "{controller}/{action}/{id}",
    defaults: new { id = UrlParameter.Optional }
);

For the url without the page value to work you have to put a default value to page:

[HttpGet]
[ResponseType(typeof(IPagedList<Pedido>))]
public async Task<IHttpActionResult> Todos(string filtro, string pagina = "")

Or you will have to overload the All method with only one parameter.

  • I edited my question the result of your answer

  • @Ricardo answered your edition.

  • Because the route works without standard parameters in the MVC project but not in the WEBAPI?

  • Does it work? I believe you’re using an older version of the framework or made some configuration that doesn’t remind you to say it’s optional. If you really can, standard MVC and the Webapi are two totally different Assembly and there will be cases where some divergences occur, even if the syntax is similar.

  • 1

    Just complementing, but I always use all optional parameters, except in cases where explicitly one of them I have to have. Moreover, in MVC it works because the MVC Binding system is different from the WEBAPI. In MVC it does everything automatically, and when it has no value, Binding puts a default value. In the API it is different as Binding understands that all parameters are mandatory.

  • edtei to Todos(string filtro = "", string pagina = "") and it worked

  • Yes, when you put default values like I commented, it will understand that you do not need to pass that parameter in the url.

  • @Ricardo, if possible, marks Gabriel’s post as a response.

Show 3 more comments

0

You are passing the wrong parameter when calling the action. In the error message you are page=1 and the action is waiting for the variable with the name pagina.

  • It was a mistake to copy and paste here, I do not leave English names here on the site.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.