In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to bind the second-level domain name to a specific controller in Asp.net Core MVC. The article is very detailed and has certain reference value. Interested friends must read it!
Application scenario: enterprise portals will set different sections according to the content. In some cases, different secondary domain names need to be set for different sectors.
In asp.net core mvc, if you want to achieve the effect of the plate, you may set up different controllers for different blocks (of course, there are other technologies, and the implementation is not discussed here). In this case, how to bind the controller to a unique second-level domain name, such as the controller corresponding to the sports channel is called SportController. When you access the system through the sports.XXX.com domain name, enter SportController directly. And other controllers cannot be accessed through this secondary domain name.
There is a routing rule configuration in asp.net core mvc, which is configured in the Startup.Configure method. The specific code is as follows:
App.UseMvc (routes = > {routes.MapRoute (name: "default", template: "{controller=Home} / {action=Index} / {id?}", defaults: new {area= "admin"});})
Unfortunately, support for domain names is not supported (as far as I know, if there is a problem, please correct it). The routing rule is registered through routes.MapRouter and added to RouteCollection. When a request comes, RouterCollection loops through all registered IRouter objects until the first matching IRouter is found. Although the framework does not support domain name configuration rules, we can implement an IRouter ourselves, in which we can implement the logic of second-level domain name judgment. Here, I temporarily name it SubDomainRouter. The specific implementation code is as follows:
Public class SubDomainRouter: RouteBase {private readonly IRouter _ target; private readonly string _ subDomain Public SubDomainRouter (string routeTemplate, RouteValueDictionary defaults, RouteValueDictionary constrains, IInlineConstraintResolver inlineConstraintResolver): base (routeTemplate, subDomain, inlineConstraintResolver, defaults, constrains, new RouteValueDictionary (null)) {if (target = = null) {throw new ArgumentNullException (nameof (target)) } if (subDomain = = null) {throw new ArgumentNullException (nameof (subDomain));} _ subDomain = subDomain; _ target = target;} public override Task RouteAsync (RouteContext context) {string domain = context.HttpContext.Request.Host.Host / / get the domain name of the current request, and compare it with _ subDomain. If you don't want to wait, ignore if (string.IsNullOrEmpty (domain) | | string.Compare (_ subDomain, domain)! = 0) {return Task.CompletedTask;} / / if the domain name matches, then verify whether the access path matches return base.RouteAsync (context). } protected override Task OnRouteMatched (RouteContext context) {context.RouteData.Routers.Add (_ target); return _ target.RouteAsync (context);} protected override VirtualPathData OnVirtualPathGenerated (VirtualPathContext context) {return _ target.GetVirtualPath (context);}}
From the above code, we can only see the domain name detection, but how to direct the domain name to a specific controller requires us to do some articles when registering the IRouter and directly add the code:
Public static class RouteBuilderExtensions {public static IRouteBuilder MapDomainRoute (this IRouteBuilder routeBuilder,string domain,string area,string controller) {if (string.IsNullOrEmpty (area) | | string.IsNullOrEmpty (controller)) {throw new ArgumentNullException ("area or controller can not be null");} var inlineConstraintResolver = routeBuilder .ServiceProvider .GetRequiredService (); string template = ""; RouteValueDictionary defaults = new RouteValueDictionary () RouteValueDictionary constrains = new RouteValueDictionary (); constrains.Add ("area", area); defaults.Add ("area", area); constrains.Add ("controller", controller); defaults.Add ("controller", string.IsNullOrEmpty (controller)? "home": controller); defaults.Add ("action", "index"); template + = "{action} / {id?}"; / / the controller information is no longer included in the path rule, but the controller name routeBuilder.Routes.Add (new SubDomainRouter (routeBuilder.DefaultHandler, domain, template, defaults, constrains, inlineConstraintResolver) required for lookup is defined by constrains; return routeBuilder;}}
Finally, we can register the corresponding rule in Startup, as follows:
Public static class RouteBuilderExtensions {public static IRouteBuilder MapDomainRoute (this IRouteBuilder routeBuilder,string domain,string area,string controller) {if (string.IsNullOrEmpty (area) | | string.IsNullOrEmpty (controller)) {throw new ArgumentNullException ("area or controller can not be null");} var inlineConstraintResolver = routeBuilder .ServiceProvider .GetRequiredService (); string template = ""; RouteValueDictionary defaults = new RouteValueDictionary () RouteValueDictionary constrains = new RouteValueDictionary (); constrains.Add ("area", area); defaults.Add ("area", area); constrains.Add ("controller", controller); defaults.Add ("controller", string.IsNullOrEmpty (controller)? "home": controller); defaults.Add ("action", "index"); template + = "{action} / {id?}"; / / the controller information is no longer included in the path rule, but the controller name required for lookup is defined by constrains (new SubDomainRouter (routeBuilder.DefaultHandler, domain, template, defaults, constrains, inlineConstraintResolver); return routeBuilder }} these are all the contents of the article "how to bind a secondary domain name to a specific controller in Asp.net Core MVC". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.