How to handle multiple parameters not required on a route

Asked

Viewed 88 times

2

I have the following wheel on an Asp.net mvc control:

[Route("video/categoria/{categoria}/{page?}/{sort?}")]

The three parameters are: categoria, filters the video category, page? is the pagination of the result, sort? is the order by of the query, more visas or more new.

First situation: First page of a category

\video\categoria\comedia

or

\video\categoria\comedia\1

Second situation: second page of a category

\video\categoria\comedia\2

Let’s take the 3 cases and put the order, and include the order parameter, that’s where I had my first problem.

\video\categoria\comedia\mais_vistos (Erro...)
\video\categoria\comedia\1\mais_vistos
\video\categoria\comedia\2\mais_vistos

I understand perfectly that the error comes the moment it tries to put "more viewed" inside the control page variable.

My solution was to put everywhere you have the link to order the page 1 before. I believe it is the right thing to do. video category comedia 1 more_visa

Now I have another problem, another parameter appeared at the end of the URL:

[Route("video/tag/{tag}/{page?}/{sort?}/{genero?}")]

This parameter will work as optional and can be (empty=all, adult, not adult)

Assuming that I gave the solution to put the page 1 whenever I want to use the order parameter, I have the following cases:

\video\categoria\comedia
\video\categoria\comedia\1
\video\categoria\comedia\2
\video\categoria\comedia\1\mais_vistos
\video\categoria\comedia\2\mais_vistos

I’m thinking of applying the same solution, only now both in the page parameter and in the Sort parameter (the default is more):

\video\categoria\comedia
\video\categoria\comedia\1\mais_vistos\adulto

\video\categoria\comedia\1
\video\categoria\comedia\1\mais_vistos\adulto

\video\categoria\comedia\2
\video\categoria\comedia\2\mais_vistos\adulto

\video\categoria\comedia\1\mais_vistos
\video\categoria\comedia\1\mais_vistos\adulto

\video\categoria\comedia\2\mais_vistos
\video\categoria\comedia\2\mais_vistos\adulto

That would be the best solution?

I would like opinions even from those who agree with the solution, leave at least one comment saying that you think the best suggestion.

Your entire experience is welcome.

UPDATING

The route refers to this method:

public async Task<ActionResult> ConsultaVideo(string categoria, int? page, string sort)

Now he must stay like this:

public async Task<ActionResult> ConsultaVideo(string categoria, int? page, string sort, string genero)
  • You can also put the headers of each Action in the question?

  • Sorry it took so long, I was having lunch, question edited with Action’s signature

1 answer

2


The problem is that you are using all parameters after the null parameter as mandatory:

public async Task<ActionResult> ConsultaVideo(string categoria, int? page, string sort, string genero)

Instead, I would define a method only with parameters in default completed, for example:

public async Task<ActionResult> ConsultaVideo(string categoria, int? page = null, string sort = "mais_vistos", string genero = "todos")

With this, the routes must work.

Addendum: if you really need to use a route without filling, better make a polyphosphism with fewer parameters and put another route in it.

  • With this solution of yours. The route to get the joke category, page 1, most viewed, and adult (here adult would not be the default), would be: adult joke video category ? That employee? No ne?

  • With this solution of yours. The route to get the joke category, page 1, most viewed, and adult (here adult would not be the default), would be: adult joke video category ? That employee? No ne?

  • No, unfortunately you can’t skip the order of the route attributes. If you need to fill gender, you also need to fill all the other.

  • So it is correct to say that if I want to take advantage of the route video category and need to put the value in genero I am obliged to put their default value in the URL, right?

  • Exactly. Or assemble a simpler polymorphism of the method with another route.

  • Perfect, thank you, it would be nice to put that last comment of yours in the reply too, it might help someone else too.

  • Okay, there it is. I thank you ;)

Show 2 more comments

Browser other questions tagged

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