我在一个网站上设置url路由,这个网站似乎可以工作,但我无法让它忽略我的javascript文件。我在使用和IIS7时也遇到了同样的问题,我没有使用ASP.NET。
我尝试了以下每种方法来忽略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文件并未加载。这快把我逼疯了!
亨利
发布于 2012-08-15 00:34:39
IIS正在接管这些文件的服务。您必须限制它们在IIS中提供服务,并忽略到该位置的任何路由。一种选择是转到IIS Filtering -> Rules,然后添加一个新规则来限制访问。但是,我相信您知道,如果您限制这些文件被提供,脚本将永远不会为客户端运行,如果这是注定要发生的,为什么不直接将物理文件移到网站之外无法访问的位置?
发布于 2012-08-15 00:44:11
为什么你不对像这样的javascript文件使用ResolveUrl呢?
<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
https://stackoverflow.com/questions/11956582
复制相似问题