繁体   English   中英

MVC IgnoreRoute /?_ escaped_fragment_ =继续使用IIS ARR进行反向代理

[英]MVC IgnoreRoute /?_escaped_fragment_= to continue Reverse Proxy with IIS ARR

技术信息

情境

我有一个AngularJS单页应用程序(SPA),我正在尝试通过外部PhantomJS服务进行预渲染。

我希望MVC的路由处理程序忽略路由/?_escaped_fragment_={fragment} ,因此该请求可以由ASP.NET直接处理 ,然后传递给IIS以代理该请求。

理论上

**我可能是错的。 据我所知,自定义路由的优先级就像注册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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM