我正在一个似乎可以正常工作的网站上设置 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 文件。这快把我逼疯了!
亨利