Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC4 List of all areas

I have an ASP.NET MVC4 application in which I am creating multiple areas, is there a way I can find out programmatically the number of areas that are present and their names.

like image 313
dev Avatar asked Mar 28 '13 19:03

dev


People also ask

What are areas in ASP.NET MVC?

ASP.NET MVC introduced a new feature called Area for this. Area allows us to partition the large application into smaller units where each unit contains a separate MVC folder structure, same as the default MVC folder structure.

What does AreaRegistration RegisterAllAreas () do?

RegisterAllAreas Method (Object) Registers all areas in an ASP.NET MVC application by using the specified user-defined information.

What is ASP area?

Areas are an ASP.NET feature used to organize related functionality into a group as a separate namespace (for routing) and folder structure (for views). Using areas creates a hierarchy for the purpose of routing by adding another route parameter, area , to controller and action or a Razor Page page .

How we can register the area in ASP.NET MVC?

When you add an area to an ASP.NET MVC application, Visual Studio creates a file named AreaRegistration. The file contains a class that derives from AreaRegistration. This class defines the AreaName property and the RegisterArea method, which registers the route information for the new area.


1 Answers

The AreaRegistration.RegisterAllAreas(); registers each area route with the DataTokens["area"] where the value is the name of the area.

So you can get the registered area names from the RouteTable

var areaNames = RouteTable.Routes.OfType<Route>()
    .Where(d => d.DataTokens != null && d.DataTokens.ContainsKey("area"))
    .Select(r => r.DataTokens["area"]).ToArray();

If you are looking for the AreaRegistration themselves you can use reflection to get types which derives from AreaRegistration in your assambly.

like image 84
nemesv Avatar answered Sep 21 '22 16:09

nemesv