gpt4 book ai didi

ASP.NET MVC IgnoreRoute()按域

转载 作者:行者123 更新时间:2023-12-04 04:28:38 27 4
gpt4 key购买 nike

更新:简洁起见重新改写...

对于ASP.NET MVC项目,是否可以使web.config重写规则优先于MVC的RegisterRoutes()调用,或者可以仅针对特定域调用IgnoreRoute

我有一个MVC应用程序,它接受多个域(mydomain.com和otherdomain.com)上的流量,该应用程序根据请求的主机(即, Multi-Tenancy )提供不同的内容。

我在web.config中配置了URL重写(反向代理),该URL重写仅适用于特定主机:

<rule name="Proxy" stopProcessing="true">
<match url="proxy/(.*)" />
<action type="Rewrite" url="http://proxydomain.com/{R:1}" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(mydomain\.com|www\.mydomain\.com)$" />
</conditions>
<serverVariables>
<set name="HTTP_X_UNPROXIED_URL" value="http://proxydomain.com/{R:1}" />
<set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
<set name="HTTP_X_ORIGINAL_HOST" value="{HTTP_HOST}" />
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule>

但是,如果通过以下方式从应用程序的 RegisterRoutes()方法中忽略了MVC应用程序,它们似乎只会接受web.config配置的路由:
routes.IgnoreRoute("proxy");

不幸的是,然后 会将忽略应用于两个域。建议非常感激...

最佳答案

can IgnoreRoute be called for just a specific domain?



是。 但是,由于默认情况下.NET路由会完全忽略域,因此您将需要自定义路由以使 IgnoreRoute特定于域。

这样做是 possible to subclass RouteBase ,最简单的解决方案是制作 custom route constraint并使用它来控制特定路由将匹配的域。路由约束可以与现有的 MapRouteMapPageRouteIgnoreRoute扩展方法一起使用,因此这是对现有配置的最小侵入性修复。

域约束
    public class DomainConstraint : IRouteConstraint
{
private readonly string[] domains;

public DomainConstraint(params string[] domains)
{
this.domains = domains ?? throw new ArgumentNullException(nameof(domains));
}

public bool Match(HttpContextBase httpContext, Route route, string parameterName,
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.QueryString["domain"];
#else
null;
#endif
if (string.IsNullOrEmpty(domain))
domain = httpContext.Request.Headers["HOST"];

return domains.Contains(domain);
}
}

请注意,出于测试目的,在 Debug模式下编译应用程序时,上述类接受查询字符串参数。这使您可以使用如下网址
http://localhost:63432/Category/Cars?domain=mydomain.com

在本地测试约束,而无需配置本地Web服务器和主机文件。此调试功能未包含在发行版中,以防止生产应用程序中可能出现的错误(漏洞)。

用法
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

// This ignores Category/Cars for www.mydomain.com and mydomain.com
routes.IgnoreRoute("Category/Cars",
new { _ = new DomainConstraint("www.mydomain.com", "mydomain.com") });

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

NOTE: There is an overload that accepts a constraints parameter on all of the built-in route extensions, including IgnoreRoute and Area routes.



引用: https://stackoverflow.com/a/48602769

关于ASP.NET MVC IgnoreRoute()按域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48793739/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com