【问题标题】:MVC IgnoreRoute /?_escaped_fragment_= to continue Reverse Proxy with IIS ARRMVC IgnoreRoute /?_escaped_fragment_= 继续使用 IIS ARR 进行反向代理
【发布时间】:2016-02-23 17:56:15
【问题描述】:

技术信息

场景

我有一个 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 的路由吗?
  • 我对@9​​87654353@ 路由的了解是否正确?
  • 我的regex 正确吗?
  • 还是我错过了什么?

【问题讨论】:

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-29
    • 2015-09-04
    • 2021-11-08
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 2013-01-28
    相关资源
    最近更新 更多