Part 79 Areas in asp net mvc

preview_player
Показать описание
Link for code samples used in the demo

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

1. Models
2. Views
3. Controllers

This structure is fine for simple applications, but when your application gets big and complex, maintaining your Models, Views and Controllers can get complicated.

1. Models
2. Views
3. Controllers
4. Routes

Let's understand this with an example.

Let's understand this with an example. Let's say, we are building a Job Portal. It's common for a typical job portal to have the following functional areas.
Employee Area - This functional area allows a job seeker to create their profile, upload resume, and perform job search.
Employer Area - This functional area allows a job provider to create employer profile, upload jobs, and search for suitable candidates.
Administrator Area - This functional area allows an administrator to configure the site and mantain.

To create an area in an MVC application
1. Right click on the project name in Solution Explorer and select Add =] Area
2. Provide a meaningful name. For example "Employee" and click Add

At this point, "Areas" folder will be created, and with in this, a folder for Emploee area will be added. You can add as many areas as you want.

In a similar fashion, add areas for Employer and Admin.

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}

At this point Add "HomeController" to the following areas. Notice that we can have a HomeController(Controller with the same) in Employee, Employer, Admin and MainArea.
1. Employee
2. Employer
3. Admin
4. Main Area

At this point, we have Index() action method in all of the HomeControllers.
public ActionResult Index()
{
return View();
}

Now Add Index view to all the areas. Copy and paste the following HTML in respective Index views.
Main Area: [h1]Main Area Index View[/h1]
Employee Area: [h1]Employee Area Index View[/h1]
Employer Area: [h1]Employer Area Index View[/h1]
Admin Area: [h1]Admin Area Index View[/h1]

At this point, build the application, and navigate to /MVCDemo. You will get an error.

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new [] { "MVCDemo.Controllers" }
);
}

Now, if you navigate to /MVCDemo/Employee, you will get an error - The resource cannot be found.

public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Employee_default",
"Employee/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

1. In Visual Studio, click Tools - Library PAckage Manager - Package Manager Console
2. In the Package Manager Console window, type the following command and press enter
Install-Package Microsoft.Web.Optimization -Pre
Рекомендации по теме
Комментарии
Автор

Thank you very much for taking time to give feedback. In the description of this video, I have included the link for ASP .NET, C#, and SQL Server playlists. All the videos are arranged in logical sequence in these playlists, which could be useful to you. Please share the link with your friends who you think would also benefit from them. If you like these videos, please click on the THUMBS UP button below the video. For email alerts, when new videos are uploaded, you may subscribe to my channel.

Csharp-video-tutorialsBlogspot
Автор

Love your videos!! You do a much better job showing the MVC beginner how to do things than any of the Microsoft employees do.

candidateresourcesinccri
Автор

Sure, will do it as soon as I can. In the description of this video, I have included the link for ASP .NET, C#, and SQL Server playlists. All the videos are arranged in logical sequence in these playlists, which could be useful to you. Please share the link with your friends who you think would also benefit from them. If you like these videos, please click on the THUMBS UP button below the video. For email alerts, when new videos are uploaded, you may subscribe to my channel.

Csharp-video-tutorialsBlogspot
Автор

Thank you, kudvenkat! That was a great explanation of Areas with thorough and complete examples!

unionblitz
Автор

Hello Venkat, Thank you so much for spending time on this tutorial and making it in a professional and clean manner. It is very easy to understand. I had attended some workshops by MS personals. But i feel the way you present concepts are great. Keep it up. All the best...

melazhi
Автор

Amazing! So very well presented and demonstrated! Love it!!

jonnywilson
Автор

Thank you so much Kudvenkat. That was a great explanation!

confesorcastilloeugenio
Автор

At first i didnt liked ur tutors, but ... you are the best tutors maker i have ever seen. Keep up. I didnt like them cuz of the common words like "vep-applications; vindows" etc :D Indian stuffs... but u are awesome :) That on 11:00 impressed me the most... i mean that u are using fresh VS from beggining and in every tutor u are using same names for ur models, views etc.. so it makes it more clear. GOOD JOB!

DimitarMitov
Автор

really ur stye of representing is awesmmm....great job Venkat...

RajuSharma-port
Автор

Thanks Venkat for sharing ur knowledge

vikasbisht
Автор

Thank you for your usefull Tutorials. Better than the book I bought.

intersowa
Автор

you are a gem, learned alot through your videos bro

jaskiratsingh
Автор

kudvenkat Thank you for your usefull Tutorials Have a good Day && Night

sergiocakmak
Автор

Thnks friend!!
Xclnt video & explanation.

mmhgarcia
Автор

you have done wonderful job, video is simply excellent and very much intuitive 

PI-Thoughts
Автор

You should change the Area names in other views. Currently If you are in Employee area and click on Employee Area only, it will take you to Main Area. Other than that lovely video.

AdvikaSamantaray
Автор

I am using VS 2013. The install command did not work for me. I had to comment out the System.Web.Optimization line in the web.config files.

trades
Автор

Thanks for the video.But if we have 100 of different functionality pages then how we ill proceed with this.Do we need to add 100 of Areas and separate Model Views and Controllers to it. Don you think it will be a very tedious task to maintain/Manage also as well as performance will degrade too. Can you please provide any other approach/efficient way to reduce such a huge code.

priyankatripathi
Автор

Good morning!
congratulations for the video!
Could you do a tutorial with entity framework?
because productivity is greater tendency for companies and freelancer.
already thanks.
Sincerely,
Izaias

izaiaszanotti
Автор

Towards the end when you're creating the links on all the different areas, couldn't you just create a partial view and pass the partial view instead of pasting all of the html? Awesome videos BTW! Thanks

prmcmusic