同一个应用程序ASP.NET Core 2.0中的不同域

我在Azure Web应用程序中托管了ASP.NET Core 2.0应用程序。

此应用已配置域domainA.com

我的应用程序中有一个路由,例如domainA.com/route 。 现在我想将其他域仅限于此路由,例如domainB.com

做这个的最好方式是什么?

实现此目的的一种方法是创建自定义路由约束以指定每个域名的路由。

DomainConstraint

public class DomainConstraint : IRouteConstraint { private readonly string[] domains; public DomainConstraint(params string[] domains) { this.domains = domains ?? throw new ArgumentNullException(nameof(domains)); } public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) { string domain = #if DEBUG // A domain specified as a query parameter takes precedence // over the hostname (in debug compile 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 (string.IsNullOrEmpty(domain)) domain = httpContext.Request.Host.Host; return domains.Contains(domain); } } 

用法

 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?}"); }); 

请注意,如果在Visual Studio中启动它,它将无法使用标准配置。 为了便于在不更改配置的情况下进行调试,可以使用域指定URL作为查询字符串参数:

 /route?domain=domaina.com 

这样您就不必重新配置IIS和本地主机文件进行调试(尽管如果您喜欢这样,您仍然可以)。 在Release版本期间,此function将被删除,因此它仅适用于生产中的实际域名。

由于路由默认响应所有域名 ,因此只有在域之间共享大量function时才这样做才有意义。 如果没有,最好为每个域设置单独的区域:

 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") } ); 

对于.net Core MVC,您可以创建新的IRouteConstraint和RouteAttribute

=> IRouteConstraint.cs

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

在您的控制器中可以像这样使用它:

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