. NET/ASP. net mvc (modular development of AraeRegistration)

Source: Internet
Author: User

Reading directory:

  • 1. Introduction

  • 2. AreaRegistration register the routing transfer routing context for modular Registration)

1. Introduction

ASP. NET Routing is very powerful and well-designed. If ASP. NETMVC is built on ASP. NET is not as accurate as ASP. NETMVC is based on Routing, so that the Controller can be found and executed smoothly;

This article briefly introduces how to use ASP. NETMVC makes good modular development, and we all know ASP. NETMVC is the UI Layer framework in a layered architecture, and the development of the UI Layer is inherently difficult to control, especially the difference between WEBUI and WINFORMUI. WEBUI has many components, it is also processed in a remote browser, so it is still a test of the architecture design;

Then ASP. what is NETMVC's AreaRegistration for? It is used to provide a good interface for us to define the Controller in other Library projects, which can be mainly used for modular development; generally, we usually define all things in WebApplication, but divide a series of Library projects according to business needs or architecture needs, so that our logical architecture is clearer and physical architecture design is flexible, such as horizontal scaling, dynamic design of business modules, and webapis;

However, Routing does not have an inevitable relationship with MVC. It can embed the desired function points in an extended way. Routing obtains the control right by providing a custom IHttpModule, MVC is based on Routing and allows Uri Routing to be used by itself. In fact, the proper extension of AreaRegistration can be used in many cases, but after all, AreaRegistration is implemented in ASP. provided in NETMVC;

2] AreaRegistration register the routing transfer routing context for modular Registration)

In a simple explanation, AreaRegistration is used in ASP. the method of registering multiple regions in NETMVC is to divide a large MVC site into multiple Area areas, and each Area has its own Controller, Action, View, and other elements; however, we generally do not do this because opening all the elements in the UI Layer of the site will bring a lot of work to the maintenance work, in addition, it is customary to place things on the UI Layer in a main WebApplication, followed by the Division of business functions, but large websites may need to do so;

2.1] Typical template method mode of AreaRegistration object structure)

Next, let's analyze the AreaRegistration object structure. At least we need to figure out how to use this object and related objects;

using System;using System.Web.Routing;namespace System.Web.Mvc{    public abstract class AreaRegistration    {        protected AreaRegistration();               public abstract string AreaName { get; }        public static void RegisterAllAreas();        public static void RegisterAllAreas(object state);        public abstract void RegisterArea(AreaRegistrationContext context);    }}

This is the code structure of the AreaRegistration object. Two static overload methods are used in Gloab. the asax file is used to Start global registration, and the object state parameter is used to pass to the object we will inherit; then we need to implement the remaining AreaName attribute and RegisterArea method;

In fact, anyone familiar with the design pattern will be familiar with this pattern, because it is the most typical template method pattern and the dependency inversion principle in the design principle. Internally, MVC only depends on the AreaRegistration object, then, the AreaRegistrationContext context is passed to the subclass to execute something through the RegisterArea method;

It should be emphasized that an AreaRegistration corresponds to an independent AreaRegistrationContext object. The relevant parameters are passed in the constructor, provided that you must implement the AreaName attribute;

2.2] AreaRegistration object implementation

Implement an OrderAreaRegistration object to inherit the abstract class of AreaRegistration;

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web.Mvc;namespace Api.Order{    public class OrderAreaRegistration : AreaRegistration    {        public override string AreaName        {            get { return "Aip/Order"; }        }        public override void RegisterArea(AreaRegistrationContext context)        {            context.MapRoute(name: "api.order.default", url: "api/order/{controller}/{action}/{orderid}",                defaults: new { controller = "OrderController", action = "GetOrderOperationDatetime", orderid = "1001" },                namespaces: new string[] { "Api.Order" });        }    }}

Define the Controller in the api project:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web.Mvc;namespace Api.Order{    public class Order : Controller    {        public string GetOrderOperationDatetime(string orderId)        {            if (orderId.Equals("1001"))                return DateTime.Now.ToString();            else                return orderId;        }    }}

Then we need to reference this Library project so that it can automatically scan our type at startup;

Figure 1:

650) this. length = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/1245415D1-0.jpg "title =" 1.jpg" width = "931" height = "313" border = "1" hspace = "1" vspace = "1" style = "width: 931px; height: 313px; "alt =" 122845484.jpg"/>

AreaRegistrationContext uses the AreaRegistraton implementation class parameter AreaName as a part of the parameter to construct a specific Context object. AreaRegistratioContext indicates the Context of a region, the animation we registered in the Context belongs to the current Area, which includes our own Route set;

Figure 2:

650) this. length = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/1245411031-1.jpg "title =" 2.jpg" width = "687" height = "133" border = "1" hspace = "0" vspace = "0" style = "width: 687px; height: 133px; "alt =" 1229302.16.jpg"/>

The site is successfully routed to the OrderController defined in the api. order project;

Figure 3:

650) this. length = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/1245414921-2.jpg "title =" 3.jpg" width = "299" height = "140" border = "1" hspace = "0" vspace = "0" style = "width: 299px; height: 140px; "alt =" 122949400.jpg"/>

In this way, the design project will also have one more choice;

Author:Wang qingpei

Source:Http://wangqingpei557.blog.51cto.com/

The copyright of this article is shared by the author and BKJIA. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide the original article connection clearly on the article page. Otherwise, you will be entitled to pursue legal liability.



This article is from the pattern driven the world blog, please be sure to keep this source http://wangqingpei557.blog.51cto.com/1009349/1308648

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.