How to separate a fixed route from a variable route with asp.net mvc5

4

I have a control that manages videos. I'm trying to do these two methods:

[Route("video/categoria/{categoria}")]
public async Task<ActionResult> Index(string categoria)
{


}

[Route("video/categoria/new-movies")]
public async Task<ActionResult> Index()
{


}

When executing the url /video/categoria/new-movies it falls on the [Route("video/categoria/{categoria}")] path by placing the new-movies value within the category variable.

I wonder if there is some form of it falling on the other route.

If it does not exist, is it best to treat everything in the same method?

I'm looking for a more technically correct solution to avoid future problems and make maintenance easier.

    
asked by anonymous 04.11.2015 / 21:34

1 answer

4

Try reversing methods:

[Route("video/categoria/new-movies")]
public async Task<ActionResult> Index()
{


}

[Route("video/categoria/{categoria}")]
public async Task<ActionResult> Index(string categoria)
{


}

Alternatively, I pass one more method, which is configuring routes in RouteConfig . Precedence always begins with the most specific route. The most generic route should last because it is last resolved.

namespace SeuProjeto
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "NovosFilmes",
                url: "video/categoria/new-movies",
                defaults: new { controller = "Videos", action = "Index" }
            );

            routes.MapRoute(
                name: "Filmes",
                url: "video/categoria/{categoria}",
                defaults: new { controller = "Videos", action = "Index", categoria = UrlParameter.Optional }
            );

            ...
        }
    }
}
    
04.11.2015 / 21:36