Mvc4 domain name ing Area, mvc4 domain name ing area

Source: Internet
Author: User
Tags subdomain

Mvc4 domain name ing Area, mvc4 domain name ing area

Environment Description

The project uses mvc4 and has an area named demo. The access path is http: // localhost: 8098/demo. You need to map demo.abc.com to the change path.

Hosts Configuration

127.0.0.1 demo.abc.com

Core code

 

Public class AreaDomainRegistrationContext {// <summary> ///// </summary> // <param name = "_ domainName"> subdomain names such: you can upload abc </param> public AreaDomainRegistrationContext (AreaRegistrationContext _ context, string _ domainName) {domainName = _ domainName; context = _ context;} private string domainName; private AreaRegistrationContext context; private RouteCollection Routes {get {if (! DomainRouteTable. domainRoutes. containsKey (domainName) {DomainRouteTable. domainRoutes [domainName] = new RouteCollection ();} return DomainRouteTable. domainRoutes [domainName] ;}} public Route MapRoute (string name, string url, object ults, object constraints = null, string [] namespaces = null) {if (namespaces = null & context. namespaces! = Null) {namespaces = context. namespaces. toArray ();} Route route = Routes. mapRoute (name, url, defaults, constraints, namespaces); route. dataTokens ["area"] = context. areaName; route. dataTokens ["UseNamespaceFallback"] = false; route. dataTokens ["SubDomain"] = domainName; return route;} public class DomainRouteTable {private static Dictionary <string, RouteCollection> _ instance = new Dictionary <st Ring, RouteCollection> (); public static Dictionary <string, RouteCollection> DomainRoutes {get {return _ instance ;}} public class DomainRoute: RouteBase {public ResolveDomainName resolveDomainName = DependencyResolver. current. getService <ResolveDomainName> (); public RouteData d = new RouteData (null, new StopRoutingHandler (); public override RouteData GetRouteData (HttpContextBase httpContext) {String domainName = resolveDomainName. Resolve (httpContext); // if (domainName = null) return null if (! DomainRouteTable. domainRoutes. containsKey (domainName) return d; var rs = DomainRouteTable. domainRoutes [domainName]; RouteData routeData = rs. getRouteData (httpContext); if (routeData = null) return d; return routeData;} public override VirtualPathData GetVirtualPath (RequestContext requestContext, RouteValueDictionary values) {string domainName = resolveDomainName. resolve (requestContext. httpConte Xt); if (domainName = null) return null; if (! DomainRouteTable. domainRoutes. containsKey (domainName) return null; var rs = DomainRouteTable. domainRoutes [domainName]; VirtualPathData vpd = rs. getVirtualPathForArea (requestContext, values); return vpd ;}} public class ResolveDomainName {public static Dictionary <string, string> DomainMap = new Dictionary <string, string> (StringComparer. ordinalIgnoreCase); public string Resolve (HttpContextBase httpContext) {string key = httpContext. request. headers ["host"]; string value; if (DomainMap. tryGetValue (key, out value) {return value;} else return null ;}}

Call Code

// Global. asax. put cs Application_Start at the beginning of RouteTable. routes. add (new DomainRoute (); ResolveDomainName. domainMap. add ("demo.abc.com: 8098", "demo"); // public class demoAreaRegistration: AreaRegistration {public override string AreaName {get {return "demo ";}} public override void RegisterArea (AreaRegistrationContext context) {context. mapRoute (name: "demo_default", url: "demo/{controller}/{action}", defaults: new {controller = "index", action = "index "}, namespaces: new string [] {"xxx. xxx "}); var context2 = new AreaDomainRegistrationContext (context," demo "); context2.MapRoute (name:" demo_default ", url:" {controller}/{action} ", ults: new {controller = "index", action = "index"}, namespaces: new string [] {"xxx. xxx "});}}

The view uses bool B = (string) ViewContext. RouteData. DataTokens ["SubDomain"]! = Null; you can determine whether the request is accessed using the demo.abc.com domain name.

Use http://demo.abc.com: 8098/and http: // localhost: 8098/demo to access Area demo

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.