Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define an endpoint route to multiple areas

I am trying to define a MapAreaControllerRoute() that routes to multiple Areas. In ASP.NET Core 3.0, however, there is the areaName: parameter that needs to be set, thus restricting each route to a single area. I don't understand how I can use one route that will work for multiple Areas.

I have read through many issues here on Stack Overflow, but it seems that this is a new requirement in ASP.NET Core 3.0. In ASP.NET Core <= 2.2 you could create a MapRoute() without defining a set areaName.

As it is now, in my Startup.cs, I define my endpoints as:

app.UseEndpoints(endpoints =>
{
  endpoints.MapAreaControllerRoute(
    name: "Area1",
    areaName: "Area1",
    pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
  );

  endpoints.MapAreaControllerRoute(
    name: "Area2",
    areaName: "Area2",
    pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
  );

  endpoints.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

});

Surely, there must be a way to define a single route to cover all Areas?

like image 428
Per Malmstedt Avatar asked Oct 12 '19 09:10

Per Malmstedt


People also ask

How can we define a route for areas?

When you add an area to a project, a route for the area is defined in an AreaRegistration file. The route sends requests to the area based on the request URL. To register routes for areas, you add code to the Global. asax file that can automatically find the area routes in the AreaRegistration file.

What is Endpoint routing?

Routing is responsible for matching incoming HTTP requests and dispatching those requests to the app's executable endpoints. Endpoints are the app's units of executable request-handling code. Endpoints are defined in the app and configured when the app starts.

How would you setup the default route using endpoints?

UseEndpoints(endpoints => { endpoints. MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); Inside the call to UseEndpoints() , we use the MapControllerRoute() method to create a route by giving the name default .

What is MVC endpoint routing?

Endpoint routing is a feature newly introduced in ASP.NET Core that enables you to provide routing information to middleware in the request processing pipeline. Before the introduction of endpoint routing, routing resolution in ASP.NET Core MVC was performed at the end of the request processing pipeline.


1 Answers

Ok, so after reading an additional bunch of links, it turns out to be a case of missing attributes for the area controllers! By tagging the controllers with the following tags:

[Area("Area1")]
[Route("Area1/[controller]/[action]")]
public class Area1Controller : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

and changing the routes to:

        app.UseEndpoints(endpoints =>
        {
                endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

            endpoints.MapAreaControllerRoute(
                name: "areas",
                areaName: "areas",
                pattern: "{area}/{controller=Home}/{action=Index}/{id?}"
                );
    }

everything seems to work as expected.

like image 92
Per Malmstedt Avatar answered Sep 19 '22 14:09

Per Malmstedt