2

I am setting up url routing on a site which seems to be working but I am unable to get it to ignore my javascript files. I have the same issue using the ASP.NET Development Server and IIS 7. I am not using mvc.

I have tried each of the following to ignore javascript with no success

    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;

Below is a basic test page I created to isolate the problem

Global.asax contains

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';
    }
}

Navigating to site/testpage/area/areaname shows the routing working but the javascript file is not loaded. This is driving me insane!

Henry

2 Answers 2

2

IIS is taking over serving those files. You would have to restrict them from being served in IIS and ignore any routes to the location. An option would be to go to IIS -> Request Filtering -> Rules and add a new rule to restrict access. I'm sure you are aware however, that if you restrict these files from being served, the script will never run for the client and if that is meant to happen, why not just move the physical files outside of the website to an unreachable location?

Sign up to request clarification or add additional context in comments.

3 Comments

I need my javascript (and css, images etc..) to be visible to the client
Well it comes down to a choice. If you want clients to be able to view images and have javascript run for your site when they visit, then it must be allowed to be served to their machine. Maybe it would help me answer more clearly if you told me why you are trying to restrict the files.
Sorry if I wasn't clear, I don't want to restrict access. Currently I have my site working using a query string like site/testpage.aspx and site/testpage.aspx?area=areaname I would like to use routing and still have my javascript run when visiting pages via a url like site/testpage and site/testpage/area/areaname I assumed this would be achieved by telling the routing to not route the javascript but maybe I've misunderstood its function.
0

Why dont you ResolveUrl for javascript files like this?

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

Also have you tried,

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

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

4 Comments

How would this help him ignore .js files?
@Mattbo: the general practice is to resolveurl on markup rather than ignoring them on global.asax
This may work but due to a large number of .js, .css files on my site I was hoping for a solution which would let me use the ASP.NET routing without having to change all my references to all my files. After reading many other cases using ignore routing on these types of files with success I assumed it is possible and that I am implementing it incorrectly. If not I will have to bite the bullet and use the ResolveUrl method. Thanks
I did not try RouteTable.Routes.IgnoreRoute("{alljs}", new {alljs=@".*\.js(/.)?"}); as IgnoreRoute is part of mvc but I did try routes.Ignore("{alljs}", new { alljs = @".*\.js(/.)?" }); which should be the asp.net equivalent. The ResolveUrl works fine for my testpage and may be my only solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.