Mvc icon indicating copy to clipboard operation
Mvc copied to clipboard

Area routing seems broken for actions with a Route Attribute

Open razzemans opened this issue 10 years ago • 1 comments

I created a new MVC project and added the following directories:

  • Areas
    • Foo
      • Controllers
        • BarController

BarController:

namespace AreaTest.Areas.Foo.Controllers
{
    [Area("Foo")]
    public class BarController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [Route("test")]
        public IActionResult Index2()
        {
            return View();
        }
    }
}

In Startup.cs:

app.UseMvc(routes =>
{

    routes.MapRoute(
        name: "fooArea",
        template: "{area:exists}/{controller=Bar}/{action=Index}");

    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");

});

I go to:

/foo => matches BarController.Index /foo/test => 404

This 404 seems unexpected. I would expect this to match the Index2 action of the controller, because it feels wrong that the value of the Area attribute does have an effect as the default route (since /foo matches) but route attributes within an Area are suddenly ignored. This would also make it behave more like non-area controllers. Yes I can fix this by adding also a [Route("foo")] to the controller, but this seems duplicate as I already defined it in the Area.

Summarized, I think the current implementation is illogical and I would expect route attributes within an Area to take the value passed to the Area attribute into account. This value passed would mean that this is a route prefix for every route attribute of its actions. If somehow not possible or wanted, maybe update the documentation of Areas to emphasize this behavior more.

razzemans avatar Aug 15 '16 15:08 razzemans

This behavior is expected. Putting a controller in an area does not affect the URL structure - you have explicitly account for that when you define your routes and that includes attribute routes. Note that there is a route token that you can use for this purpose (ex. [Route("[area]/test")]).

danroth27 avatar Aug 15 '16 16:08 danroth27