[英]MVC IgnoreRoute /?_escaped_fragment_= to continue Reverse Proxy with IIS ARR
技术信息
AngularJS
单页应用 Umbraco 7.3.0
网站,已扩展为通过Umbraco.Core.ApplicationEventHandler
在单独的类库中注册路由 情境
我有一个AngularJS
单页应用程序(SPA),我正在尝试通过外部PhantomJS
服务进行预渲染。
我希望MVC
的路由处理程序忽略路由/?_escaped_fragment_={fragment}
,因此该请求可以由ASP.NET
直接处理 ,然后传递给IIS
以代理该请求。
理论上
Umbraco
基于ASP.NET MVC
构建。 System.Web.Routing.RouteCollection
类配置路由。 Umbraco
时,通过System.Web.Routing.RouteTable
配置的任何路由都将优先于Umbraco路由,因此, Umbraco
永远不会对其进行处理** **我可能是错的。 据我所知,自定义路由的优先级就像注册Umbraco
路由之前所做的那样。 但是我不确定告诉MVC
忽略路由是否还会阻止Umbraco处理该路由。
实践中
我尝试使用以下命令忽略路由:
尝试之一:
routes.Ignore("?_escaped_fragment_={*pathInfo}");
这将引发错误: The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.
The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.
尝试两次:
routes.Ignore("{*escapedfragment}", new { escapedfragment = @".*\?_escaped_fragment_=\/(.*)" });
这并没有导致错误,但是Umbraco仍然收到了请求并将我递回了我的根页面。 Regexr上的正则表达式验证 。
问题
MVC
可以根据其query string
实际忽略路由吗? Umbraco
的路线了解是否正确? regex
正确吗? 内置的路由行为不考虑查询字符串。 但是,路由是可扩展的,并且可以根据需要基于查询字符串。
最简单的解决方案是创建一个自定义RouteBase
子类,该子类可以检测您的查询字符串,然后使用StopRoutingHandler
确保路由不起作用。
public class IgnoreQueryStringKeyRoute : RouteBase
{
private readonly string queryStringKey;
public IgnoreQueryStringKeyRoute(string queryStringKey)
{
if (string.IsNullOrWhiteSpace(queryStringKey))
throw new ArgumentNullException("queryStringKey is required");
this.queryStringKey = queryStringKey;
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
if (httpContext.Request.QueryString.AllKeys.Any(x => x == queryStringKey))
{
return new RouteData(this, new StopRoutingHandler());
}
// Tell MVC this route did not match
return null;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
// Tell MVC this route did not match
return null;
}
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// This route should go first
routes.Add(
name: "IgnoreQuery",
item: new IgnoreQueryStringKeyRoute("_escaped_fragment_"));
// Any other routes should be registered after...
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.