Detailed description of the Area feature in ASP. net mvc 2

Source: Internet
Author: User

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

 
 
  1. Website/Index  
  2. Admin/ Index  
  3. User/ Index  
  4. ……/…… 

Usually, several directories corresponding to the Controller are created under Views, and the aspx page is placed in the directory.

 
 
  1. Views\Website\Index  
  2. Views\Admin\Index  
  3. Views\User\Index  
  4. 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:

 
 
  1. Website/Product/Index  
  2. Website/Catalog/Index  
  3. 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

 
 
  1. Website\Product\Index  
  2. Website\Catalog\Index  
  3. Website\Order\Index  
  4. 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

 
 
  1. Areas\Website\Views\Product  
  2. Areas\Website\Views\ Catalog  
  3. Areas\Website\Views\ Order  
  4. 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

 
 
  1. Protected void Application_Start ()
  2. {
  3. AreaRegistration. RegisterAllAreas ();
  4. // Register the region Url rules. Pay attention to the order
  5. RegisterRoutes (RouteTable. Routes );
  6. }
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.

 
 
  1. namespace Valor.Asmyna.Areas.Website  
  2.  
  3. {  
  4.  
  5.     public class HomeController : Controller  
  6.  
  7.     {  
  8.  
  9. public ActionResult Index()  
  10.         {  
  11.             ViewData["title"] = "Website/Home/Index";  
  12.             return View();  
  13.         }  
  14.     }  
  15. public class ProductController : Controller  
  16.     {  
  17.         public ActionResult Index()  
  18.         {  
  19. ViewData["title"] = "Website/Product/Index";  
  20. return View();       }  
  21.     }  
  22. public class ContentController : Controller  
  23.     {  
  24.         public ActionResult Index()  
  25.         {  
  26.   ViewData["title"] = "Website/Content/Index";  
  27.     return View();  
  28.         }   }  

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

 
 
  1. namespace Valor.Asmyna.Areas.Home{  
  2.    public class HomeController : Controller  
  3.     {  
  4.        public ActionResult Index()  
  5.         {  
  6.             ViewData["title"] = "Home/Content/Index";  
  7.             return View();  
  8.         }   }  
  9. public class ProductController : Controller  
  10.     {  
  11.         public ActionResult Index()  
  12.         {  
  13.             ViewData["title"] = "Home/Content/Index";  
  14.             return View();  
  15.         }}  
  16.     public class ContentController : Controller  
  17.     {  
  18.         public ActionResult Index()  
  19.         {  
  20.             ViewData["title"] = "Home/Content/Index";  
  21.             return View();  
  22.         }  
  23.     }}  
  24. namespace Valor.Asmyna.Areas.Home  
  25. {  
  26.     public class HomeController : Controller  
  27.     {  
  28.         public ActionResult Index()  
  29.         {  
  30.             ViewData["title"] = "Home/Home/Index";  
  31.             return View();  
  32.         }   }  
  33. public class ProductController : Controller{  
  34.        public ActionResult Index()  
  35.         {  
  36.             ViewData["title"] = "Home/Product/Index";  
  37.             return View();  
  38.         }    }  
  39.     public class ContentController : Controller  
  40.     {  
  41.         public ActionResult Index()  
  42.         {  
  43.             ViewData["title"] = "Home/Content/Index";  
  44.             return View();  
  45.         }  
  46.     }  

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.