filmov
tv
Part 79 Areas in asp net mvc

Показать описание
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
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
Комментарии