[ASP.NET
MVC Mavericks Road]08 - Area Using
ASP.NET MVC allows the use of Areas (areas) to organize Web applications. Each Area represents a different aspect of the application. functional module. This is very useful for large projects. Area allows each functional module to have its own folder with its own Controller, View and Model, but it also adds a certain degree of difficulty to management.
Directory of this article
Create Area
Right-click the project and select Add->Area, the following dialog box for filling in Area will pop up:
After clicking Add, the project directory structure is as follows:
Similar to creating an empty MVC project structure, the Admin Area has its own Controllers, Models and Views folders, the difference is that there is an additional AdminAreaRegistration.cs file. This file defines a class called AdminAreaRegistration. Its content is as follows:
namespace MvcApplication1.Areas.Admin { public class AdminAreaRegistration : AreaRegistration { public override string AreaName { get { return "Admin"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Admin_default", "Admin/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); } } }
AdminAreaRegistration automatically generated by the system The class inherits from the abstract class AreaRegistration and overrides the
AreaName property and RegisterArea method. In the RegisterArea method, it defines a default route for us. We can also define other routes specific to the Admin Area in this method. But one thing to note is that if you want to name the route here, make sure it is different from the entire application.
The MapRoute method of the AreaRegistrationContext class is used the same as the MapRoute method of the RouteCollection class, except that the AreaRegistrationContext class limits the registered routes to only match the controller of the current Area. Therefore, if you add the The default namespace of the controller has been changed, and the routing system will not be able to find this controller.
The RegisterArea method does not require us to call it manually. The Application_Start method in Global.asax already has the following code to do this for us:
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
Call the AreaRegistration.RegisterAllAreas method Let the MVC application, upon startup, look for all classes that inherit from AreaRegistration and call their RegisterArea method for each such class.
Note: Do not change the order of registration methods in Application_Start easily. If you put the RouteConfig.RegisterRoutes method before the AreaRegistration.RegisterAllAreas method, the registration of Area routes will be after the route registration. The routing system is in order. matching, so doing so will cause the Controller requesting the Area to match the wrong route.
Area operation
Adding controller, view and model in Area is the same as general addition. Here, we add a controller named Home in the Admin Area, the code is as follows:
public class HomeController : Controller { public ActionResult Index() { return View(); } }
Then we add a View for the Index Acton, the code is as follows:
@{ ViewBag.Title = "Index"; Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <p> <h2 id="Admin-nbsp-Area-nbsp-Index">Admin Area Index</h2> </p> </body> </html>
Run application, and then locate the URL to /Admin/Home/Index. The following is the running result:
Here, We have seen that the workflow in Area is actually the same as the process in the root directory. But Area is not a completely independent workspace, let's take a look below.
Controller ambiguity issue
Just imagine, if we now also add a Controller named Home in the Controller folder of the root directory, then we By positioning the URL to /Home/Index, can the routing system match the Controller in the root directory?
After adding the HomeController to the Controllers folder in the root directory, add a View for the Index. The content is arbitrary:
... <body> <p> <h2 id="Root-nbsp-Index">Root Index</h2> </p> </body> ...
The route will not be changed. We use the default route defined by the system in the RouteConfig.cs file. :
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 } ); }
Run the program and locate the URL to /Home/Index. As a result, we will see the following error message:
出现这个问题是因为路由系统进行匹配的时候出现了Controller同名的歧义。
当Area被注册的时候,Area中定义的路由被限制了只寻找 Area 中的Controller,所以我们请求 /Admin/Home/Index 时能正常得到 MvcApplication1.Areas.Admin.Controllers 命名空间的 HomeController。然而我们在RouteConfig.cs文件的RegisterRoutes方法中定义的路由并没有类似的限制。
为了解决这个问题,我们需要在RouteConfig.cs文件中定义的路由中加上对应的 namespaces 参数。RouteConfig.cs 中修改后的路由如下:
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[] { "MvcApplication1.Controllers" } ); }
运行程序,如下结果说明解决了同名歧义问题:
添加了 namespaces 参数后,路由系统在对这个路由进行匹配时,优先匹配指定命名空间的controller,如果匹配到则即刻停止查找,如果在指定的命名空间下没有匹配到对应的controller,再按照一般的方式进行匹配。
生成Area URL链接
关于Area的URL链接生成,可以分为这么三种情况:第一种是在当前Area生成指向当前Area的链接;第二种是生成指向其他Area的链接;第三种是在某个Area中生成指向根目录的链接。下面是这三种情况生成链接的方法,使用的路由定义是系统默认的。
如果要在Area中生成当前Area的URL链接,直接用下面的方法就行:
@Html.ActionLink("Click me", "About")
它根据当前所在的Area和Controller会生成如下Html代码:
<a href="/Admin/Home/About">Click me</a>
如果要生成其他Area的URL链接,则需要在Html.ActionLink方法的匿名参数中使用一个名为area的变量来指定要生成链接的Area名称,如下:
@Html.ActionLink("Click me to go to another area", "Index", new { area = "Support" })
它会根据被指定的Area去找路由的定义,假定在Support Area中定义了对应的路由,那么它会生成如下链接:
<a href="/Support/Home/Index">Click me to go to another area</a>
如果要在当前Area生成指根目录某个controller的链接,那么只要把area变量置成空字符串就行,如下:@Html.ActionLink("Click me to go to top-level part", "Index", new { area = "" })
它会生成如下Html链接:<a href="/Home/Index">Click me to go to top-level part</a>
以上就是[ASP.NET MVC 小牛之路]08 - Area 使用的内容,更多相关内容请关注PHP中文网(www.php.cn)!

C# and .NET provide powerful features and an efficient development environment. 1) C# is a modern, object-oriented programming language that combines the power of C and the simplicity of Java. 2) The .NET framework is a platform for building and running applications, supporting multiple programming languages. 3) Classes and objects in C# are the core of object-oriented programming. Classes define data and behaviors, and objects are instances of classes. 4) The garbage collection mechanism of .NET automatically manages memory to simplify the work of developers. 5) C# and .NET provide powerful file operation functions, supporting synchronous and asynchronous programming. 6) Common errors can be solved through debugger, logging and exception handling. 7) Performance optimization and best practices include using StringBuild

.NETFramework is a cross-language, cross-platform development platform that provides a consistent programming model and a powerful runtime environment. 1) It consists of CLR and FCL, which manages memory and threads, and FCL provides pre-built functions. 2) Examples of usage include reading files and LINQ queries. 3) Common errors involve unhandled exceptions and memory leaks, and need to be resolved using debugging tools. 4) Performance optimization can be achieved through asynchronous programming and caching, and maintaining code readability and maintainability is the key.

Reasons for C#.NET to remain lasting attractive include its excellent performance, rich ecosystem, strong community support and cross-platform development capabilities. 1) Excellent performance and is suitable for enterprise-level application and game development; 2) The .NET framework provides a wide range of class libraries and tools to support a variety of development fields; 3) It has an active developer community and rich learning resources; 4) .NETCore realizes cross-platform development and expands application scenarios.

Design patterns in C#.NET include Singleton patterns and dependency injection. 1.Singleton mode ensures that there is only one instance of the class, which is suitable for scenarios where global access points are required, but attention should be paid to thread safety and abuse issues. 2. Dependency injection improves code flexibility and testability by injecting dependencies. It is often used for constructor injection, but it is necessary to avoid excessive use to increase complexity.

C#.NET is widely used in the modern world in the fields of game development, financial services, the Internet of Things and cloud computing. 1) In game development, use C# to program through the Unity engine. 2) In the field of financial services, C#.NET is used to develop high-performance trading systems and data analysis tools. 3) In terms of IoT and cloud computing, C#.NET provides support through Azure services to develop device control logic and data processing.

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
