2

我正在一个似乎可以正常工作的网站上设置 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>

测试页.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()); }
    }

}

测试.js

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

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

亨利

4

2 回答 2

2

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

于 2012-08-14T16:34:39.467 回答
0

你为什么不使用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

于 2012-08-14T16:44:11.447 回答