ASP. net mvc uses a second-level domain name to register an area

Source: Internet
Author: User

For more information about ASP. net mvc implementation of second-level domain names, see blog: http://www.cnblogs.com/luanwey/archive/2009/08/12/1544444.html.

Now I want to talk about how to use the routing engine (System. Web. Routing) To automatically register regions for multiple views. For example, a regional website, such as 58 and Ganji, has sub-stations in every city. The pages of cities in every city are roughly the same, but data is retrieved from every city! Now we need to complete a function. For example, the URL cs.8fdc.com loads the view of Changsha, while sz.8fdc.com loads the view of Shenzhen. The view of each city may have different structural locations, so you must write the view of the city separately.

Therefore, the function we need is: cs.8fdc.com should be matched to {area} .8fdc.com! : Match the view in the CD or CS folder on areas.

Microsoft provides a method for registering the view in areas:

Public class arearouting: arearegistration {public override string areaname {get {return "CS" ;}} public override void registerarea (arearegistrationcontext context) {context. maproute ("default1", // route name "CS/{controller}/{action}/{ID}", // URL with parameters new {Action = "Index ", id = urlparameter. optional}, null, new string [] {"HB. controllers. website "} // select the namespace, Which is HB. controllers. website namespace);} public static void registerroutes (routecollection routes) {// routes. ignoreroute ("{resource }. axd/{* pathinfo }");}}

Inherit from arearegistration. If you use http: // localhost/CS/..., the region will be registered as CS and matched to this route.

But what we need is http://cs.8fdc.com/....that is, we need to use http: // {area} .8fdc.com.ASP. net mvc implements second-level domain nameArticleWe can do this.

 
Routes. add ("domainroute", new domainroute ("{area} .8fdc.com", // domain with parameters <br/> "{controller}/{action}/{ID }", // URL with parameters new {controller = "home", Action = "Index", id = ""}, // parameter defaults null, new string [] {"HB. controllers. website "}));

But in fact, we cannot register a region. We only need to write the area in route data, but not the area of data tokens. You can use routedebug. DLL to observe the route information! So we need to modify the domainroute method! Modify this in the domainroute. CS class

// Route data routedata DATA = NULL; If (domainmatch. Success & pathmatch. Success) {DATA = new routedata (this, routehandler); // Add the default option if (ults! = NULL) {foreach (keyvaluepair <string, Object> item in defaults) {data. values [item. key] = item. value; # region write area and namespaces to If (item. key. equals ("area") | item. key. equals ("namespaces") {data. datatokens [item. key] = item. value ;}# endregion }}// match the domain name routing for (INT I = 1; I <domainmatch. groups. count; I ++) {group Group = domainmatch. groups [I]; If (group. success) {string key = DOMA Inregex. groupnamefromnumber (I); If (! String. isnullorempty (key )&&! Char. isnumber (Key, 0) {If (! String. isnullorempty (group. value) {data. values [Key] = group. value; # region adds if (key. equals ("area") {data. datatokens [Key] = group. value ;}# endregion }}// match the domain name path for (INT I = 1; I <pathmatch. groups. count; I ++) {group Group = pathmatch. groups [I]; If (group. success) {string key = pathregex. groupnamefromnumber (I); If (! String. isnullorempty (key )&&! Char. isnumber (Key, 0) {If (! String. isnullorempty (group. value) {data. values [Key] = group. value; # region adds if (key. equals ("area") {data. datatokens [Key] = group. value ;}# endregion }}} return data;

then add in global. asax

Public static void registerroutes (routecollection routes) {routes. ignoreroute ("{resource }. axd/{* pathinfo} "); routes. add ("domainrouteformanage", new domainroute ("manage.8fdc.com", // fixed second-level domain name "{controller}/{action}/{ID }", // URL with parameters new {area = "manage", controller = "home", Action = "Index", id = "", namespaces = new string [] {"HB. controllers. manage "}}// parameter defaults); routes. add ("domainrouteformutiwebsite", new domainroute ("{area} .8fdc.com", // {area} serves as the second-level domain name "{controller}/{action}/{ID }", // URL with parameters new {area = "CS", controller = "home", Action = "Index", id = "", namespaces = new string [] {"HB. controllers. website "}}// parameter defaults); routes. maproute ("default", // route name "{area}/{controller}/{action}/{ID }", // URL with parameters new {area = "CS", controller = "home", Action = "Index", id = urlparameter. optional}, null, new string [] {"HB. controllers. website "} // default parameter );}

The first routes. Add is to use a fixed second-level domain name to register a region,

The second routes. Add uses {area} as the second-level domain name.

At last, routes. maproute is the default routing method. If the region is not registered, the view in the default views under the root directory is directly used.

This rule allows multiple regions to use the same controller without rewriting.The method in arearegistration can dynamically register a region based on the area name. If we add a city site, just copy the CS folder in areas!

ProjectSource code:Http://download.csdn.net/detail/jobily/3668911

Published on csdn, because the number on csdn is missing, I hope you can add points for me!

A link to 51cto is attached:Http://down.51cto.com/data/262527

The article is poorly written. You can download it.CodeYou can ask questions about the study!

Digress: I have always said that I am too lazy to write articles in my blog. This feature has been written for several months. It has been said that it will be released only now, and the article is still so messy and written throughout the night, I really don't have the talent to write! However, you still have to write something and share it with everyone for progress!



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.