开发者

Confused by Use of ~ in ASP.NET

I am programming for the first time in ASP.NET. It's proven to be very easy and I'm really enjoying it. One feature I like is that, within a web page, I can reference files from root by using the tilde (~) character and then the rest of the path. However, this does not seem to be consistent. For example, it works within the context of href, e开发者_开发技巧.g.

<link href="~/css/StyleSheet.css" />

It doesn't work with respect to src, e.g.

<img src="~/images/header.jpg" />

Why is this? Is there something I'm doing wrong. The inconsistency is annoying.


From my experience the ~ trick only works in server controls. If your img has runat="server" that should fix it.


ASP.NET Web Site Paths

To overcome these disadvantages, ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application. You can use the ~ operator in conjunction with folders to specify a path that is based on the current root.

...

You can use the ~ operator in any path-related property in server controls. The ~ operator is recognized only for server controls and in server code. You cannot use the ~ operator for client elements.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜