Domaine différent dans la même application ASP.NET Core 2.0

J’ai l’application ASP.NET Core 2.0 hébergée dans l’application Web Azure.

Cette application a configuré le domaine domainA.com .

J’ai un itinéraire dans mon application, par exemple domainA.com/route . Maintenant, je veux épingler un autre domaine uniquement à cette route, par exemple domainB.com .

Quelle est la meilleure façon de procéder?

Une façon d’y parvenir est de créer une contrainte de route personnalisée pour spécifier les routes qui fonctionnent pour chaque nom de domaine.

DomainConstraint

public class DomainConstraint : IRouteConstraint { private readonly ssortingng[] domains; public DomainConstraint(params ssortingng[] domains) { this.domains = domains ?? throw new ArgumentNullException(nameof(domains)); } public bool Match(HttpContext httpContext, IRouter route, ssortingng routeKey, RouteValueDictionary values, RouteDirection routeDirection) { ssortingng domain = #if DEBUG // A domain specified as a query parameter takes precedence // over the hostname (in debug comstack only). // This allows for testing without configuring IIS with a // static IP or editing the local hosts file. httpContext.Request.Query["domain"]; #else null; #endif if (ssortingng.IsNullOrEmpty(domain)) domain = httpContext.Request.Host.Host; return domains.Contains(domain); } } 

Usage

 app.UseMvc(routes => { routes.MapRoute( name: "DomainA", template: "route", defaults: new { controller = "DomainA", action = "Route" }, constraints: new { _ = new DomainConstraint("domaina.com") }); routes.MapRoute( name: "DomainB", template: "route", defaults: new { controller = "DomainB", action = "Route" }, constraints: new { _ = new DomainConstraint("domainb.com") }); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); 

Notez que si vous lancez ceci dans Visual Studio, cela ne fonctionnera pas avec la configuration standard. Pour permettre un débogage facile sans modifier la configuration, vous pouvez spécifier l’URL avec le domaine en tant que paramètre de chaîne de requête:

 /route?domain=domaina.com 

C’est juste pour que vous n’ayez pas à reconfigurer IIS et votre fichier hosts local pour le débogage (bien que vous puissiez le faire si vous préférez). Pendant une Release cette fonctionnalité est supprimée, elle ne fonctionnera donc qu’avec le nom de domaine réel en production.

Étant donné que les itinéraires répondent à tous les noms de domaine par défaut, il est judicieux de le faire de cette manière si vous avez une grande quantité de fonctionnalités partagées entre les domaines. Sinon, il est préférable de configurer des zones séparées pour chaque domaine:

 routes.MapRoute( name: "DomainA", template: "{controller=Home}/{action=Index}/{id?}", defaults: new { area = "DomainA" }, constraints: new { _ = new DomainConstraint("domaina.com") } ); routes.MapRoute( name: "DomainA", template: "{controller=Home}/{action=Index}/{id?}", defaults: new { area = "DomainB" }, constraints: new { _ = new DomainConstraint("domainb.com") } ); 

Pour .net Core MVC, vous pouvez créer une nouvelle IRouteConstraint et un RouteAtsortingbute.

=> IRouteConstraint.cs

 using System; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace Microsoft.AspNetCore.Routing { public class ConstraintHost : IRouteConstraint { public ssortingng _value { get; private set; } public ConstraintHost(ssortingng value) { _value = value; } public bool Match(HttpContext httpContext, Route route, ssortingng parameterName, RouteValueDictionary values, RouteDirection routeDirection) { ssortingng hostURL = httpContext.Request.Host.ToSsortingng(); if (hostURL == _value) { return true; } //} return false; //return hostURL.IndexOf(_value, SsortingngComparison.OrdinalIgnoreCase) >= 0; } public bool Match(HttpContext httpContext, IRouter route, ssortingng routeKey, RouteValueDictionary values, RouteDirection routeDirection) { throw new NotImplementedException(); } } public class ConstraintHostRouteAtsortingbute : RouteAtsortingbute { public ConstraintHostRouteAtsortingbute(ssortingng template, ssortingng sitePermitted) : base(template) { SitePermitted = sitePermitted; } public RouteValueDictionary Constraints { get { var constraints = new RouteValueDictionary(); constraints.Add("host", new ConstraintHost(SitePermitted)); return constraints; } } public ssortingng SitePermitted { get; private set; } } } 

Et dans votre contrôleur peut l’utiliser comme ça:

  [ConstraintHostRoute("myroute1/xxx", "domaina.com")] [ConstraintHostRoute("myroute2/yyy", "domainb.com")] public async Task MyController() { return View(); }