Part 38. Areas in .NET Core MVC. | asp-area | ASPNETCOREMVC. | Asp-area routing and folder structure

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome to programming concepts my name is amit and this is part 38 of asp.net core mvc tutorial in this video we will talk about what are arials in dot net core mvc this is continuation to part 37 anchor tag helper attributes in dot net core mbc so please watch it before proceeding to this one i shared the link in the description first of all let's read the definition from microsoft areas are an asp.net feature used to organize related functionality into a group as a separate namespace for routing folder structure for views and razer pages using areas creates a hierarchy for the purpose of routing by adding another route parameter area to the controller and action or eraser page let's understand what that means what microsoft is trying to achieve with areas if you see in our existing project we are creating functionality to manage employ so basically employee management portal kind of thing to achieve this we created controllers views models and other stuff like routing separate layout view imports etc now let's say we have a new requirement where we need to provide functionality for blocks now to implement this we required a controller views and models but if you imagine the block functionality it is a separate app or feature in itself so instead of creating its controller and other stuff within existing folder structure make your project complicated in long run areas provide a way to partition an asp.net core web app into smaller functional groups each with its own set of laser pages controller views and models an area is effectively a structure inside an app and an asp.net coreweb project logical component like pages model controllers and view are kept in different folders the asp.net core runtime uses naming convention naming convention to create the relationship between these components for a large app it may be advantageous to partition the app into separate high level areas or functionalities all right enough theory let's see how to use area in our project let's get back to visual studio and we need to implement blocks so first create a new folder and name it areas then within this folder we need to create area for our blog but before that let's see another important thing just right click on any other folder let's say on controller click on add and you can see the option to create area is not available now right click on areas click on add and you can see the option to create an area is at the top so a normal folder you won't get this option but on folder with name areas visual studio recognize that we are going to use this folder for what purpose all right click on it select mvc area and let's name it blocks first it will updating dependency information then it will apply scaffolding and will generate folder structure for us it also generates scaffolding readme.txt file for us let's read the content scaffolding has generated all the files and added the required dependencies however the application startup code which is startup.cs class may require additional changes for things to work end to end add the following code to the configure method in your applications startup class if not already done it clearly says files and required dependencies have been added but we need to make few more changes and it will provide a sample route on how to create routes for areas we'll cover them in just a bit first let's have a look at the folder it has created for us so in solution explorer you can see it generated four folders for us of which we are already aware of those are model view controller and data let's quickly create one controller for block so right click add controller select mvc hyphen controller hyphen empty and click on add leave the name as home controller and click on add let's add a view as well so right click on index add view and then select razer view empty and click on it and leave the name as it is and click on it let's specify the message say within h1 message from index view comma home controller and blog area so till now we just created a new area and named it blocks and created a controller and view if you look at our startup.cs class you can see that we already configured our routes to find the controller and action method so if i run this application and navigate to home slash index then it should find our controller and work without any issue right let's see that let's run the program navigate to home slash index oops exception application not able to find the view and if you look at the location it is searching it is in root view folder so to overcome this let's specify the absolute path of our view stop the application and while returning view specify tilde sign for root slash areas slash blocks slash views slash home slash index dot cs html and let's run the program navigate to home slash index and working as expected but absolute path not worth it because we may have same controller and action method in our root controller folder as well because we will have different name spaces right which can throw ambiguous match exception at runtime right try it by yourself let's try to achieve the same thing with the help of areas next we need to prepare our middleware and routes in such a way that our application can find these areas let's go to startup.cs class just copy and paste the existing route change the name from default to areas within pattern just append area colon exists within curly braces which means if first placeholder in url match with area then this route will execute otherwise this route will be skipped and our default route will execute simple next we need to decorate our controller with area attribute let's go to home controller so area within square bracket and you can see it accept area name as an input parameter let's give blocks as an area name and let's run the program now if we follow our routing pattern for our areas it first required an area name which is blocks then controller which is home and finally action which is index press enter and you can see working as expected in this way we can have a controller with same name in the areas folder as well as in root controller folder and because of the routes it will not conflict with each other all right and finally how can we use areas with our anchor element let's see that let's go to the layout page copy and paste ali element change asp controller to home asp action to index text to blocks and finally use asp hyphen area and our area name is blocks save and navigate to our default page and click on blocks and working as expected all right let's get back to our slide and summarize what we had done so far consider using areas in a project when the app is made of multiple high level functional components that can be logically separated you want to partition the app so that each functional area can be worked on independently and these steps we had performed so far we created a folder structure for areas where scaffolding has been applied then we prepare routes in our middleware and we use area attribute in our controller and finally used anchor tag helper attribute for navigation so basically separation of concern you can also create a separate layout page and view start page for each area explore these things by yourself by creating and calling these pages alright then that's it in this video if you have any queries related to the content of this video do ask me in comments till then thanks for watching [Music] you
Info
Channel: PRO Concepts
Views: 4,806
Rating: undefined out of 5
Keywords: asp.net core, asp.net core tutorial, asp net mvc tutorial, asp.net mvc core tutorial, asp.net mvc core course, dot net core tutorial, dot net core mvc tutorial, .net core, Anchor Tag helper in Asp.Net Core, anchor tag helper, tag helpers, asp-all-route-data, aspnetcore without entity framework, asp-route-{value}, asp-route, asp-fragment, Anchor Tag Helper Attributes, dotnet core, proconcepts, asp-area, areas in asp.net core mvc, asp.net core mvc areas, area in asp.net core mvc
Id: 5znYzo36t4s
Channel Id: undefined
Length: 11min 33sec (693 seconds)
Published: Sun Jun 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.