0
votes

I have a controller with an action method and I have configured attribute routing:

[RoutePrefix("foos")]
public class FooController : BaseController
{
        [HttpGet]
        [Route("")]
        public ActionResult List()
        {
            return View();
        }
}

Here's routing configuration:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapMvcAttributeRoutes();

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

Everything works fine. When I navigate to http://webPageAddress/foo/ my action is called and list is returned.

Now I want to make this route default. I've added new attribute so:

[HttpGet]
[Route("~/")]
[Route("")]
public ActionResult List()
{
    return View();
}

The result is default route (http://webPageAddress/) works, but the old one (http://webPageAddress/foo/) doesn't work anymore (http 404 code).

How can I mix it and have both configured properly?

1

1 Answers

1
votes

You need to make sure the route for http://webPageAddress/foo/ is registered before the route for http://webPageAddress/. With attribute routing, the only way to do this is to use the Order property to set the order.

[HttpGet]
[Route("~/", Order = 2)]
[Route("", Order = 1)]
public ActionResult List()
{
    return View();
}

Reference: Understanding Routing Precedence in ASP.NET MVC and Web API