BKJIA interviewed Microsoft MVP Yi Mingzhi in January. The main topic is ASP. net mvc 2. We once talked about the Area feature. Here we will organize this article for you to understand the Area in ASP. net mvc 2. For more information about ASP. net mvc, see ASP. net mvc Framework video tutorial.
Mvc2 pr2 was installed recently. After reading the instructions, it highlights the updated area project. I am sorry to share my thoughts with you. You are welcome to make a brick.
Handling of the same problem before Areas
In the mvc1.0 era, websites should be differentiated by the directory structure. For example
- Website/Index
- Admin/ Index
- User/ Index
- ……/……
Usually, several directories corresponding to the Controller are created under Views, and the aspx page is placed in the directory.
- Views\Website\Index
- Views\Admin\Index
- Views\User\Index
- Views\.......\.......
In this way, several directories are created.
In fact, there is nothing wrong with this. The only bad thing is that with the business needs, there will be more and more structural requirements, and there will be more and more folders under the views directory, or you need more detailed page paths, for example:
- Website/Product/Index
- Website/Catalog/Index
- Website/Contect/Index
Of course, you can use UrlRouteing or ViewEngine to solve these problems. But there is no doubt that as the website runs for a long time, there will be more and more files under the same Controller Directory, which will bring a lot of trouble for the naming of actionresults under the same Controller and maintenance in UrlRouting. It makes management inconvenient for individuals to understand ].
After Areas is available, this problem is mitigated. Or the above Url
- Website\Product\Index
- Website\Catalog\Index
- Website\Order\Index
- Website\Contact\Index
You can use the Area added by mvc2.0 to solve this problem.
Create a project
First, use mvc2 to create a new project, create the Areas folder under the root directory of the Website, and create the directories to be differentiated in the Areas folder, such as the Website in this example, then add the Views directory under the Website directory, add the class management Controller directory and create aspx files under the views directory. Create a file structure
- Areas\Website\Views\Product
- Areas\Website\Views\ Catalog
- Areas\Website\Views\ Order
- Areas\Website\Views\ Contact
Copy web. config to the current new views directory in the original default views directory. You can even delete the original views directory now.
Create Areas region UrlRouting
Create a new class and inherit the abstract class of AreaRegistration.
Modify Global. sas
- Protected void Application_Start ()
- {
- AreaRegistration. RegisterAllAreas ();
- // Register the region Url rules. Pay attention to the order
- RegisterRoutes (RouteTable. Routes );
- }
Create a Controller class for the region page
There is no difference in creating a Controller class for the region page. You can create a Controller class on another external class library project, the only note is that the namespace must be consistent with the namespace of the class that registers the Area rule. We know that Controller is not subject to namespace constraints when Areas is not used. That is to say, if you have a Controller name, and no matter which namespace it is in, it works. If we create two identical Controller class names in different namespaces, there will be no errors during compilation, but when running the mvc website, two identical Controller classes will be prompted, and the system does not know which one to use. However, Areas has some limitations. It must have the same namespace as the namespace of the AreaRegistration class. For example, the project namespace of my AreaRegistration website is Valor. asmyna. areas. website Then I separate the Controller as an independent class library. If I write a namespace randomly, this Controller does not work for views in the Area, however, he takes effect on the Controller of the original Views directory and only sets his namespace to Valor. asmyna. areas. website. xxx. the leading role of xxx takes effect.
- namespace Valor.Asmyna.Areas.Website
-
- {
-
- public class HomeController : Controller
-
- {
-
- public ActionResult Index()
- {
- ViewData["title"] = "Website/Home/Index";
- return View();
- }
- }
- public class ProductController : Controller
- {
- public ActionResult Index()
- {
- ViewData["title"] = "Website/Product/Index";
- return View(); }
- }
- public class ContentController : Controller
- {
- public ActionResult Index()
- {
- ViewData["title"] = "Website/Content/Index";
- return View();
- } }
- }
OK. Test it in the browser.
Problems with completely consistent Area STRUCTURE
We continue to add a Home directory under the "Area" directory and three identical controller directories under the "Veiws" directory.
Register an Area rule directly in the registered Website AreaRegistration namespace. The default Controller is Home .,
Access two paths:
/Website/Product
/Home/Product
At this time, the controller can work for the views of the two area directories. The printed results on the page are consistent.
Obviously, this is not correct. As a result, we just thought of the Area Controller's namespace restriction problem. Let's register them separately. Modify the AreaRegistration namespace in the Home region and create a Controller class for HomeArea to make their namespaces consistent. This time we use Valor. Asmyna. Areas. Website
- namespace Valor.Asmyna.Areas.Home{
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- ViewData["title"] = "Home/Content/Index";
- return View();
- } }
- public class ProductController : Controller
- {
- public ActionResult Index()
- {
- ViewData["title"] = "Home/Content/Index";
- return View();
- }}
- public class ContentController : Controller
- {
- public ActionResult Index()
- {
- ViewData["title"] = "Home/Content/Index";
- return View();
- }
- }}
- namespace Valor.Asmyna.Areas.Home
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- ViewData["title"] = "Home/Home/Index";
- return View();
- } }
- public class ProductController : Controller{
- public ActionResult Index()
- {
- ViewData["title"] = "Home/Product/Index";
- return View();
- } }
- public class ContentController : Controller
- {
- public ActionResult Index()
- {
- ViewData["title"] = "Home/Content/Index";
- return View();
- }
- }
- }
After compilation, the access is processed by the Controller.
Home/Product
Website/Product
Original article title: Use the area in mvc2 to make the website more organized
Link: http://www.cnblogs.com/tthxnz/archive/2009/11/12/1602097.html