In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the Area features in ASP.NET MVC 2". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the Area features in ASP.NET MVC 2".
The treatment of the same problem before Areas
In the age of mvc1.0, if you want to distinguish websites by directory structure. For example
Website/Index Admin/ Index User/ Index... /.
Usually several directories corresponding to Controller are created under Views, and then aspx pages are placed in them.
Views\ Website\ Index Views\ Admin\ Index Views\ User\ Index Views.
Create a number of directories in this way
In fact, there is nothing wrong with this. The bad thing may be that with the needs of the business, there will be more and more structural requirements, more and more folders under the views directory, or you need more detailed page paths, such as:
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 in the same Controller directory, which brings a lot of trouble to the naming of ActionResult under the same Controller and the maintenance in UrlRouting. Bring inconvenience to management [personal understanding].
Now, with the advent of Areas, this problem has been alleviated. Or the Url as above
Website\ Product\ Index Website\ Catalog\ Index Website\ Order\ Index Website\ Contact\ Index
You can use the new Area of mvc2.0 to solve this problem.
Set up a project
First of all, use mvc2 to create a new project, establish an Areas folder under the root of the site, establish a directory that you want to distinguish between the Areas folder, such as the Website in this example, and then continue to add the Views directory under the Website directory and continue to add the need to classify the management of Controller directories and establish aspx files under the views directory. To form a file structure.
Areas\ Website\ Views\ Product Areas\ Website\ Views\ Catalog Areas\ Website\ Views\ Order Areas\ Website\ Views\ Contact
Copy web.config from the original default views directory to the current new views directory, and you can even delete the original views directory now
Establish Areas area UrlRouting
Anywhere, create a new class that inherits the AreaRegistration implementation of the abstract class
Modify Global.sas
Protected void Application_Start () {AreaRegistration.RegisterAllAreas (); / / Registration region Url rules, note the order RegisterRoutes (RouteTable.Routes);}
Create Controller classes for regional pages
Creating a Controller class for a zone page is no different and can be built on another external class library project. * note that the namespace needs to be consistent with the leading namespace of the class that registers the Area rule. We know that Controller is not constrained by namespace when not using Areas. That is to say, as long as you have a Controller name, and no matter which namespace it is in, it can work. If we build two identical Controller class names in different namespaces, there will be no error when compiling, but running the mvc website will prompt that there are two identical Controller classes, and the system does not know which one to use. But Areas has some restrictions, making sure that the leading namespace of the namespace is the same as the namespace of the AreaRegistration class. For example, the AreaRegistration website project namespace I built is Valor.Asmyna.Areas.Website, and then I separate Controller as a separate class library. If I write any namespace at random, this Controller does not work for the views in Area, but it works for the Controller of the original Views directory. It only works if his namespace is set as the leader of Valor.Asmyna.Areas.Website.xxx.xxx.
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, go to the browser to test it.
Problems that will arise when the structure of Area is completely consistent
We continue to add a Home directory under the Area directory and three identical controller directories under his Veiws directory
Register an Area rule for him directly in the Website AreaRegistration namespace, using the default system default Controller as Home.
Access to 2 paths:
/ Website/Product
/ Home/Product
At this time, controller can work on the views of both area directories. The results printed on the page are consistent.
It is obvious that this is wrong. As a result, we just thought of the problem of the namespace limitation of choosing names for Area's Controller. Let's register them separately. Modify the namespace of AreaRegistration 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 ();}
Access after compilation, each handling for their own Controller
Home/Product
Website/Product
Thank you for your reading, the above is the content of "what is the Area feature in ASP.NET MVC 2". After the study of this article, I believe you have a deeper understanding of what the Area feature in ASP.NET MVC 2 is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.