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

时间:2016-02-23 17:56:16

标签: c# asp.net regex asp.net-mvc umbraco

技术信息

方案

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

我希望MVC的路由处理程序忽略路由/?_escaped_fragment_={fragment},因此request can be handled directly by 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.

尝试两个:

routes.Ignore("{*escapedfragment}", new { escapedfragment = @".*\?_escaped_fragment_=\/(.*)" });

这并没有导致错误,但是Umbraco仍然接收了请求并将我的根页交给了我。 Regex validation on Regexr

问题

  • MVC实际上可以忽略基于query string
  • 的路线
  • 我对Umbraco路由的了解是否正确?
  • 我的regex是否正确?
  • 或者我错过了什么?

1 个答案:

答案 0 :(得分:2)

内置路由行为不考虑查询字符串。但是,路由是可扩展的,并且可以根据需要基于查询字符串。

最简单的解决方案是创建一个可以检测查询字符串的自定义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 }
        );
    }
}
相关问题