问题描述
我在Azure应用服务上托管了一个ASP.NET Core 2.0应用.
I have an ASP.NET Core 2.0 app hosted on an Azure App Service.
此应用程序绑定到domainA.com
.我的应用程序中有一条路由,例如domainA.com/route
.
This application is bound to domainA.com
. I have one route in my app—for example, domainA.com/route
.
现在,我想介绍另一个域,但让它仅对不同的路由做出响应,例如domainB.com
.
Now, I want to introduce another domain, but have it respond only to a different route—for example, domainB.com
.
做到这一点的最佳方法是什么?
What is the best way to do this?
推荐答案
一种实现此目的的方法是制作自定义路由约束,以指定每个域名适用的路由.
One way to accomplish this is to make a custom route constraint to specify which routes function for each domain name.
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指定为查询字符串参数:
Note that if you fire this up in Visual Studio it won't work with the standard configuration. To allow for easy debugging without changing the configuration, you can specify the URL with the domain as a query string parameter:
/route?domain=domaina.com
这只是这样,您不必重新配置IIS和本地主机文件即可进行调试(尽管您仍然可以这样做).在Release
构建过程中,此功能已删除,因此仅在生产中的实际域名下可用.
This is just so you don't have to reconfigure IIS and your local hosts file to debug (although you still can if you prefer that way). During a Release
build this feature is removed so it will only work with the actual domain name in production.
由于默认情况下路由会响应所有域名,因此只有在域之间共享大量功能的情况下,这样做才有意义.如果没有,最好为每个域设置单独的区域:
Since routes respond to all domain names by default, it only makes sense to do it this way if you have a large amount of functionality that is shared between domains. If not, it is better to setup separate areas for each domain:
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") }
);
这篇关于ASP.NET Core 2.0中基于域的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!