Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

URL Routing and Stylesheets/Javascript

0.00/5 (No votes)
24 Feb 2010 2  
When working with the new URL routing portion of the ASP.NET 4.0, there is a gotcha that needs to be worked around.Assume a website with the following structure:~/Images/header.jpg~/Stylesheets/style.css~/Scripts/scripts.js~/Driver/Details.aspx~/Default.aspxAlso assume you...
When working with the new URL routing portion of the ASP.NET 4.0, there is a gotcha that needs to be worked around.

Assume a website with the following structure:
~/Images/header.jpg
~/Stylesheets/style.css
~/Scripts/scripts.js

~/Driver/Details.aspx
~/Default.aspx

Also assume you use URL routing to change the structure of the page
/Driver/Details/{CustomerNumber} maps to ~/Driver/Details.aspx

When including the <script> and <link> tags in your details.aspx page, if you use the normal "../Stylesheets/style.css", then the stylesheets wont load, nor will the scripts because the page would be looking for "/Driver/Stylesheets/style.css".

Instead, use the <%= ResolveClientURL("~/Stylesheets/style.css") %> method to define the source of the resource.

Example:
XML
<link href='<%= ResolveClientUrl("~/StyleSheets/ui.tabs.css") %>' rel="stylesheet"
    type="text/css" />
<pre lang="xml"><script src='<%= ResolveClientUrl("~/Scripts/ui.core.js") %>' type="text/javascript"></script>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here