Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Conventional routing with custom templates not working when you have area attributes #7959

Closed
@davidliang2008

Description

@davidliang2008

Is this a Bug or Feature request?:

Bug

Steps to reproduce (preferably a link to a GitHub repo with a repro project):

https://github.com/davidliang2008/DL.Issues.AspNetCore.Mvc.CustomRouting

Description of the problem:

When you want a more user/SEO friendly URLs on your site, you would set up custom routings to use slugs instead of ids.

public void Configure(IApplicationBuilder app, IHostingEnvironmentt env)
{
    ...

    app.UseMvc(routes =>
    {
        routes.MapRoute(
                name: "productListByTypeRoute",
                template: "products/{type}",
                defaults: new { area = "", controller = "products", action = "listByType" }
        );

        routes.MapRoute(
                name: "productListByCategoryRoute",
                template: "products/{type}/{category}",
                defaults: new { area = "", controller = "products", action = "listByCategory" }
        );

        routes.MapRoute(
                name: "productDetailsRoute",
                template: "products/{type}/{category}/{product}",
                defaults: new { area = "", controller = "products", action = "details" }
        );

        routes.MapRoute(
                name: "areaRoute",
                template: "{area:exists}/{controller=dashboard}/{action=index}/{id?}"
        );

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

    ...
}

And then I have the controller and action setup to handle those requests.

public class ProductsController : Controller
{
     ...

    public async Task<IActionResult> ListByType(string type)
    {
        ...
        return View(...);
    }

    public async Task<IActionResult> ListByCategory(string type, string category)
    {
        ...
        return View(...);
    }

    public async Task<IActionResult> Details(string type, string category, string product)
    {
        ...
        return View(...);
    }

    ...
}

So the following links are expected to call their corresponding actions:

Link Action Parameters
/products/countertop ListByType type=countertop
/products/countertop/2cm-granite ListByCategory type=countertop, category=2cm-granite
/products/countertop/2cm-granite/imperial-splendor Details type=countertop, category=2cm-granite, product=imperial-splendor

Everything works fine until you add an area! All custom URLs would return 404!

[Area("admin")]
public abstract class AdminControllerBase : Controller {}
public class DashboardController : AdminControllerBase
{
    public IActionResult Index()
    {
        return View();
    }
}

Notes:

  1. With the area routing setup in Startup, as long as the [Area("admin")] is commented out, all custom links would work with the setting new { area = "", ... }.
  2. If you have [Area("admin")] on, the only way to get those custom links to work is to remove area = "" from custom routing setup.
  3. This setup worked flawlessly in previous Microsoft.AspNetCore.Mvc 2.0.7!

Version of Microsoft.AspNetCore.Mvc or Microsoft.AspNetCore.App or Microsoft.AspNetCore.All:

V2.1.0

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions