UPDATE: Included simple SiteMap support in sample here.
If you’ve played with the new ASP.NET routing stuff with WebForms, you probably encountered some issues with relative paths to static content (css, Javascript files etc). This is a common issue across browsers because of the way they resolve relative paths. Here’s an example:
<script src="Javascript/TestScript.js" type="text/javascript"></script>
Let’s say this was in a page called Index.html. Your browser request might look like this:
http://www.chriscavanagh.com/Chris/SimpleRoutingTest/Index.html
After grabbing the page content, the browser would see the relative path to TestScript.js and (reasonably) assume it could retrieve it from here:
http://www.chriscavanagh.com/Chris/SimpleRoutingTest/Javascript/TestScript.js
However, in our case we’ve thrown some funky URL routing into the mix. Here’s how we want to hit our page:
http://www.chriscavanagh.com/Chris/SimpleRoutingTest/Search/42
Assume for now we’ve defined a Route to divert that to Index.html (in Global.asax):
routes.Add( new Route( "Search/{category}", new WebFormRouteHandler( "~/Index.html" ) ) );
The routing works as expected, but when the browser tries to get TestScript.js, here’s where it’ll go looking:
http://www.chriscavanagh.com/Chris/SimpleRoutingTest/Search/Javascript/TestScript.js
Fortunately there are some simple ways to fix this
(note you may need to adapt these slightly to your specific case):
- Use ASP.NET themes and keep Javascript in embedded resources. The theme mechanism takes care of paths nicely; it knows how to find everything from the application root and "just works". Make Javascript files embedded resources and use ScriptManager.RegisterClientScriptResource to serve them up automatically through ScriptManager.axd.
- Use the <base> element in your page and set its href to an absolute URL. This can go in each page, or just in you Master page(s) if you use them. You could add code similar to this:
public string BaseUrl { get { return Request.Url.GetLeftPart( UriPartial.Authority ) + VirtualPathUtility.ToAbsolute( "~/" ); } }
- Then make your <base> element look like this:
<base href="<%= BaseUrl %>" />
That should give you an absolute URL to your application root. As long as all paths are relative to that, everything should work nicely
I’ve updated my "Simple Routing Test" site to show this in action (source available here). In this case the BaseUrl helper (above) is in the RoutablePage class. Each page shows a bunch of links (dynamically generated against the defined routes). At the bottom you should see "Hello from Javascript" with a yellow background. The yellow signifies the CSS is working, and the text shows it’s successfully invoked a Javascript function
Hope this helps!


11 comments
Comments feed for this article
November 7, 2008 at 2:47 am
ASP.NET Routing… Goodbye URL rewriting? « Chris Cavanagh’s Blog
[...] of this. I’ve also added a related comment to the end of this post > Added a new post that might help you resolve issues with relative file paths when using routes. Check it out [...]
November 7, 2008 at 7:39 am
Dew Drop - November 7, 2008 | Alvin Ashcraft's Morning Dew
[...] ASP.NET Routing + Just Enough Rope (Chris Cavanagh) [...]
November 11, 2008 at 5:59 am
Leigh
I simply use the ResolveClientUrl method to get around this problem:
<script src=”" type=”text/javascript”>
November 11, 2008 at 6:47 am
Chris Cavanagh
Leigh – Thanks for the comment
What would ResolveClientUrl return if called from as ASPX / ASCX contained in a subfolder? (glancing at MSDN suggests the URL it returns is relative to the current page / control location).
Seems a pretty thorny problem in general:; check out this old post http://www.pluralsight.com/community/blogs/fritz/archive/2006/02/06/18596.aspx
November 11, 2008 at 6:49 am
Chris Cavanagh
Leigh – I’ll give ResolveUrl / ResolveClientUrl a try and update here if they work better
Thanks!
August 11, 2009 at 2:33 pm
Royce
How do you configure this to work with IIS 5
December 22, 2009 at 6:33 am
Routing ASP.NET | ЧПУ в ASP.NET | Создание "красивых" урл-адресов в ASP.NET | WEBFeria
[...] http://chriscavanagh.wordpress.com/2008/04/25/systemwebrouting-with-webforms-sample/ http://chriscavanagh.wordpress.com/2008/11/06/aspnet-routing-just-enough-rope/ http://andir-notes.blogspot.com/2009/02/url-rewriting-systemwebrouting.html [...]
March 1, 2010 at 3:45 am
Ashish Jain
Hi,
You can also use script manager,
This is what i am using and its working very good
March 1, 2010 at 3:46 am
Ashish Jain
March 1, 2010 at 6:28 am
Chris Cavanagh
Ashish – Sorry, WordPress’s comments don’t handle html/xml very well. If you escape the brackets (< and >) it should work ok…
Thanks for posting!
June 15, 2010 at 5:19 pm
Greg
Hi Chris,
I’m trying to follow your example to get a webforms site to use the page routing. It now gets css + js correctly but the server controls are still broken. They now seem to be referring to http://localhost rather than http://localhost/website
Are you able to point out the error of my ways?
Greg