开发者

Referencing links in a different way

I am new to C# and asp.net and would like to know the following: Can I reference jquery library in the following format?

src="http://~/sites/booksite/tools/js/jquery.tools.min.js"

it does not give a compilation error or anything b开发者_运维技巧ut want to make sure..

Thank you


The ~/ (tilde+slash) method of referencing paths is an ASP.NET thing - URLs on elements with runat="server" (i.e. server-side controls) will be evaluated and expanded from the relative path (where ~/ is the root of the application or virtual directory.) If the ASP.NET engine isn't doing this, then it doesn't get done.

In order to specify a relative path from the root, you should be able to get away with just the slash:

src="/sites/booksite/tools/js/jquery.tools.min.js"

Alternatively, apply the runat="server" value, and it would work:

runat="server" src="~/sites/booksite/tools/js/jquery.tools.min.js"

But when using the tilde+slash, then http:// won't work.


No. if the jquery is local to your site you can use the ~ to represent the root of your site. Provided that this src attribute is on a control with runat=server. But providing the http:// is unneeded in that case.


No you must definetly cannot. The "root folder" of your "application" is managed by the server, your application doesn't care if it's in / (development machine) or /prettyapp (production server).

What you can do however is ask ASP.NET figure out the path for you and fill it in:

<script src='<%= ResolveUrl("~/sites/booksite/tools/js/jquery.tools.min.js") %>'></script>

As a node, don't just use absolute paths (/something/) like Mr. Disappointment suggests, your application will die if you deploy it to a virtual directory.


This isn't the right way.

The Tilde will only be processed if the tag has runat="server", so yes you can do it but you shouldn't.

The ONLY reason you would want to specify a full http path to the script file is if you are hosting it via a content delivery network (CDN).

However, jQuery is already hosted on arguably the largest CDN -> Google. See http://code.google.com/apis/libraries/devguide.html

So, I would just leverage the resources that they give for free.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜