Handling a number of non-mandatory parameters on a route

2

I have the following wheel in an asp.net mvc control:

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

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

First situation: First page of a category

\video\categoria\comedia

or

\video\categoria\comedia

Second situation: second page of a category

\video\categoria\comedia

Let's get the 3 cases and place the order, and include the order parameter, that's where I had my first problem.

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

I fully understand that the error comes at the time it tries to put "more_views" inside the control's page variable.

My solution was to put everywhere you have the link with order \ 1 \ page before. I think it's the right thing to do.     \ video \ category \ comedy \ 1 \ more_views

Now I have another problem, one more 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, nonadditional)

Starting from the principle that I gave the solution to put \ 1 \ of page whenever I want to use the order parameter, I happen to have the following cases:

\video\categoria\comedia
\video\categoria\comedia
\video\categoria\comedia
\video\categoria\comedia\mais_vistos
\video\categoria\comedia\mais_vistos

I'm thinking of applying the same solution, only now in both the page parameter and the sort parameter (the default is more_looked):

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

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

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

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

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

Would that be the best solution anyway?

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

The whole experience of you is welcome.

UPDATE

The route refers to this method:

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

Now it should look like this:

public async Task<ActionResult> ConsultaVideo(string categoria, int? page, string sort, string genero)
    
asked by anonymous 26.11.2015 / 15:12

1 answer

2

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

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

In its place, I would define a method with only parameters in default filled, for example:

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

Having this, the routes should work.

Addendum: If you really need to use an unfilled route, better do a polimosphism with fewer parameters and put another route on it.

    
26.11.2015 / 15:58