【问题标题】:ASP.NET 4.0 Routing Ignore .js .css etcASP.NET 4.0 路由忽略 .js .css 等
【发布时间】:2012-08-14 16:18:51
【问题描述】:

我正在一个网站上设置 url 路由,该网站似乎可以正常工作,但我无法让它忽略我的 javascript 文件。我在使用 ASP.NET 开发服务器和 IIS 7 时遇到了同样的问题。我没有使用 mvc。

我已尝试以下各项来忽略 javascript,但均未成功

    routes.Add(new Route("Scripts/", new StopRoutingHandler()));
    routes.Add(new Route("Scripts/{*pathInfo}", new StopRoutingHandler()));
    routes.Add(new Route("{resource}.js", new StopRoutingHandler()));
    routes.Add(new Route("{resource}.js/{*pathInfo}", new StopRoutingHandler()));
    routes.Add(new Route("*.js", new StopRoutingHandler()));
    routes.Ignore("Scripts/{*pathInfo}");
    routes.Ignore("{file}.js");
    routes.Ignore("{resource}.js/{*pathInfo}");
    routes.Ignore("Scripts/{*pathInfo}");
    routes.Ignore("{folder}/{*pathInfo}", new { folder = "Scripts" });
    routes.Ignore("{*alljs}", new { alljs = @".*\.js(/.*)?" });
    RouteTable.Routes.Ignore("{resource}.js/{*pathInfo}");
    RouteTable.Routes.RouteExistingFiles = false;

以下是我创建的用于隔离问题的基本测试页面

Global.asax 包含

void Application_Start(object sender, EventArgs e) {
    RegisterRoutes(RouteTable.Routes);
}

void RegisterRoutes(RouteCollection routes){
    routes.Ignore("{*alljs}", new { alljs = @".*\.js(/.*)?" });        
    routes.MapPageRoute("testpage", "testpage", "~/testpage.aspx");
    routes.MapPageRoute("area", "testpage/area/{name}", "~/testpage.aspx");
}

testpage.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testpage.aspx.cs" Inherits="testpage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test.aspx</title>
        <script type="text/javascript" src="./Scripts/test.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <p id="jsLoaded">false</p>
    </form>
</body>
</html>

testpage.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class testpage : System.Web.UI.Page{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.RouteData.Values.ContainsKey("name")) { Response.Write(Page.RouteData.Values["name"].ToString()); }
    }

}

test.js

window.onload = function () {
    if (document.getElementById('jsLoaded')) {
        document.getElementById('jsLoaded').innerHTML = 'test.js loaded';
    }
}

导航到 site/testpage/area/areaname 显示路由工作但未加载 javascript 文件。这让我发疯了!

亨利

【问题讨论】:

    标签: asp.net routing


    【解决方案1】:

    IIS 正在接管这些文件的服务。您必须限制它们在 IIS 中提供服务,并忽略到该位置的任何路由。一个选项是转到 IIS -> 请求过滤 -> 规则并添加一个新规则来限制访问。但是,我相信您知道,如果您限制提供这些文件,则脚本将永远不会为客户端运行,如果要发生这种情况,为什么不将网站之外的物理文件移动到无法访问的位置?

    【讨论】:

    • 我需要我的 javascript(和 css、图像等)对客户端可见
    • 这归结为一个选择。如果您希望客户在访问时能够查看图像并为您的网站运行 javascript,则必须允许将其提供给他们的机器。如果你告诉我你为什么要限制文件,也许它会帮助我更清楚地回答。
    • 对不起,如果我不清楚,我不想限制访问。目前,我的网站使用像 site/testpage.aspx 和 site/testpage.aspx?area=areaname 这样的查询字符串工作/testpage/area/areaname 我认为这可以通过告诉路由不路由 javascript 来实现,但也许我误解了它的功能。
    【解决方案2】:

    你为什么不ResolveUrl 这样的 javascript 文件?

    <script type='text/javascript' src='<%= ResolveUrl("~/Scripts/test.js") %>'></script>
    

    你也试过了,

    RouteTable.Routes.IgnoreRoute("{*alljs}", new {alljs=@".*\.js(/.*)?"});
    

    参考:http://haacked.com/archive/2008/07/14/make-routing-ignore-requests-for-a-file-extension.aspx

    【讨论】:

    • 这将如何帮助他忽略 .js 文件?
    • @Mattbo:一般做法是在标记上解析url,而不是在 global.asax 上忽略它们
    • 这可能有效,但由于我的网站上有大量 .js、.css 文件,我希望有一个解决方案可以让我使用 ASP.NET 路由而无需更改所有引用到我所有的文件。在阅读了许多其他案例后,在这些类型的文件上使用忽略路由并成功,我认为这是可能的,并且我没有正确地实现它。如果不是,我将不得不硬着头皮使用 ResolveUrl 方法。谢谢
    • 我没有尝试 RouteTable.Routes.IgnoreRoute("{alljs}", new {alljs=@".*\.js(/.)?"}) ;因为 IgnoreRoute 是 mvc 的一部分,但我确实尝试了 routes.Ignore("{alljs}", new { alljs = @".*\.js(/.)?" });这应该是 asp.net 等价的。 ResolveUrl 适用于我的测试页,可能是我唯一的解决方案。
    猜你喜欢
    • 2012-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 2013-06-28
    • 2011-07-30
    • 1970-01-01
    相关资源
    最近更新 更多