Ian Boyd
Ian Boyd

Reputation: 257047

How to specify path relative to site root?

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

Upvotes: 5

Views: 2032

Answers (1)

Michael Dunlap
Michael Dunlap

Reputation: 4310

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>

Upvotes: 6

Related Questions