Modify the order of registering areas in MVC and control the operations of areas.

Source: Internet
Author: User
I. Preface

First, someone has to ask, why should we change the registration order of areas?

When you use areas, an identifier is usually added to the front, for example, admin/{controller}/{action}/{ID}

The registration of areas is earlier than the default website (that is, the website not in the areas folder)

 

However, I think, can I simply put all the parts in areas instead of the external default website?

Then, remove one of the identifiers and change them to the default website.

At this time, two folders, controller and view, are missing from the project root directory.

Is it neat? Indeed... But the problem also arises.

 

 

2. A big problem of putting all websites in Areas

Simply put, this problem is that you cannot specify the registration order of different areas. What will happen?

If I have two areas:

One is an identifier, called Admin. The address is admin/{controller}/{action}/{ID}

The default one is web. The address is {controller}/{action}/{ID}

At this time, if the web registration is earlier than the admin, the problem will occur. Even if I enter Admin. aspx, it will be captured by the web in order and is considered as a controller in the Web

So we need to find a way to change the registration order of areas.

 

Why cannot the order be determined by default? Let's take a look at the MVC source code. It uses reflection to get areas. Put it in a set and register it with foreach.

 

 

3. Two Methods for setting the registration order

1) The first method was to read an article about areas that Boyou wrote yesterday. After I asked him below, he told me

Portal. You can see the following message: www.cnblogs.com/terrysun/archive/2010/04/13/1742418.html.

The method is to move the registration of areas out and put it in the same place, so that the order can be changed at will.

 

2) The second method is the one I just tested yesterday. Although it seems troublesome, it seems clearer. I personally recommend

First, I overwrite an arearegistration class, which is used to register the areas

Arearegistrationorder

 /// <Summary>
/// Register areas in order
/// </Summary>
Abstract Public class arearegistrationorder: arearegistration
{
/// <Summary>
/// Store areacontent
/// </Summary>
Protected static list <arearegistrationcontext> areacontent = new list <arearegistrationcontext> ();

/// <Summary>
/// Store arearegistration
/// </Summary>
Protected static list <arearegistrationorder> arearegistration = new list <arearegistrationorder> ();

/// <Summary>
/// Hijack
/// </Summary>
/// <Param name = "context"> </param>
Public override void registerarea (arearegistrationcontext context)
{
Areacontent. Add (context );
Arearegistration. Add (this );
}

/// <Summary>
/// Register
/// </Summary>
/// <Param name = "context"> </param>
Public abstract void registerareaorder (arearegistrationcontext context );

/// <Summary>
/// Order
/// </Summary>
Public abstract int order {Get ;}

/// <Summary>
/// Register areas in order
/// </Summary>
Public static void registerallareasorder ()
{
Registerallareas ();
Register ();
}

/// <Summary>
/// Register
/// </Summary>
Private Static void register ()
{
List <int []> order = new list <int []> ();
For (int K = 0; k <arearegistration. Count; k ++)
{
Order. Add (New int [] {arearegistration [K]. Order, k });
}
Order = order. orderby (O => O [0]). tolist ();
Foreach (VAR o in order)
{
Arearegistration [O [1]. registerareaorder (areacontent [O [1]);
}
}
}
}

 

Then how can we use this abstract class?

You only need to change two places

1) first, you need to replace the previously registered areas function in global. asax and delete the code in the registerroutes function.

(There is an error in the comment, which should be "Code also deleted", not "code page deleted")

 

2) Next, you can create a new areas. For the newly created areas, you need to make the following changes:

To sum up the total things you need to do:

. Replace the original inherited class, change to the class above, and implement Abstract Functions.

... Delete registerarea and change it to registerareaorder. The original operation is written in registerareaorder.

... In order, specify the order. The smaller the value, the earlier the execution is. It can be a negative number.

 

 

Iv. Ending

Source code download: http://files.cnblogs.com/dozer/MVC-AREAS.zip

Thank you for your reading. If you have any ideas, click here for your support ~ Thank you ~

 

 

 


This work is licensed using the knowledge sharing signature-non-commercial use of the 3.0 unported license agreement.

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.