5

i want to specify a path to a file relative to the root of the web-site. e.g.

<script type="text/javascript" src="/Scripts/jquery-1.7.2.min.js"></script>

In ASP.net that causes problems because the "root" of the site can be different from the root of server.

e.g. Specifying src results in a GET from path

src="/Scripts/jquery-1.7.2.min.js"
http://localhost:64276/Scripts/jquery-1.7.2.min.js                           404

src="~/Scripts/jquery-1.7.2.min.js"
http://localhost:64276/WebSite/Adminstration/~/Scripts/jquery-1.7.2.min.js   404

src="~Scripts/jquery-1.7.2.min.js"
http://localhost:64276/WebSite/Adminstration/~Scripts/jquery-1.7.2.min.js    404

src="~/Scripts/jquery-1.7.2.min.js" runat="server"
500 Unexpected character '$'

How can i specify a site relative path when using HTML in ASP.net?

See also

Community
  • 1
  • 1
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219

1 Answers1

6

If you're using ASP.net MVC, then src="~/scripts/blahblah" should work fine.

If you're not using MVC, then you'll need to use something like:

<script src='<%= this.ResolveClientUrl("~/Scripts/jquery-1.7.2.min.js") %>' type="text/javascript"></script>
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Michael Dunlap
  • 4,300
  • 25
  • 36
  • 1
    The `<%= this.ResolveClientUrl("~/Scripts/jquery-1.7.2.min.js") %>` worked perfectly. ***Perfectly!*** – Ian Boyd Nov 21 '12 at 21:42