0

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.