【问题标题】:How to ignore route in asp.net forms url routing如何忽略asp.net表单中的路由url路由
【发布时间】:2010-09-21 08:49:48
【问题描述】:

我正在使用 .NET 3.5 SP1 框架,并且在我的应用程序中实现了 URL 路由。我收到了 javascript 错误:

Error: ASP.NET Ajax client-side framework failed to load.
Resource interpreted as script but transferred with MIME type text/html.
ReferenceError: Can't find variable: Sys

我认为这是因为我的路由选择了 microsoft axd 文件,而没有正确发送 javascript。我做了一些研究,发现我可以使用Routes.IgnoreRoute,这应该可以让我忽略下面的axd:

Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

但是,当我将该行添加到我的 Global.asax 时,我收到了这个错误:

CS1061: 'System.Web.Routing.RouteCollection' does not contain a definition for 'IgnoreRoute' and no extension method 'IgnoreRoute' accepting a first argument of type 'System.Web.Routing.RouteCollection' could be found (are you missing a using directive or an assembly reference?)

我已经导入了 System.Web.Routing 命名空间,有什么想法吗?

【问题讨论】:

    标签: c# asp.net url-routing axd ignoreroute


    【解决方案1】:

    您不需要参考 ASP.NET MVC。您可以使用 StopRoutingHandler 来实现 IRouteHandler ,如下所示:

    routes.Add(new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));
    

    这是 .NET 3.5 SP1 的一部分,不需要 MVC。 IgnoreRoutes 方法是一种方便的扩展方法,它是 ASP.NET MVC 的一部分。

    【讨论】:

    • 如何忽略 ASP.NET Core (MVC 6) 中的 *.php 文件?这是一个很大的不同!
    【解决方案2】:

    一个老问题,但如果它仍然可以帮助任何人,这对我有用:

    routes.Ignore("{resource}.axd/{*pathInfo}");
    

    “Ignore”方法存在,而在标准 ASP.NET 中“IgnoreRoute”方法似乎不存在(即不使用 MVC)。这将达到与 Haacked 的代码相同的结果,但更简洁...

    【讨论】:

    • 是的,但忽略方法从 .Net 4 开始可用。OP 使用的是 3.5 SP1。
    【解决方案3】:

    我想补充一点,您还需要确保您的 IgnoreRoutes 规则的顺序正确,否则您的第一个路由将首先应用,而您的 IgnoreRoute 将...被忽略。

    【讨论】:

      【解决方案4】:

      MapRoute 和 IgnoreRoute 是 System.Web.Mvc 中的扩展方法 --- 您是否正确引用了该程序集?

      【讨论】:

      • 我没有使用 MVC,所以我没有引用该程序集。我是否需要单独下载 MVC 程序集才能使用 IgnoreRoute,或者它应该是 .NET 3.5 SP1 的一部分?
      猜你喜欢
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多