Confused about using ~ in ASP.NET

I am programming for the first time in ASP.NET. It turned out to be very easy, and I really enjoy it. One of the features that I like is that on the webpage I can link to files from root using the tilde character (~) and then the rest of the path. However, this does not seem consistent. For example, it works in the href context, for example.

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

This does not work with respect to src, for example.

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

Why is this? I'm doing something wrong. The mismatch is annoying.

+4
source share
2 answers

In my experience, the trick ~ only works in server management tools. If your img has runat="server" , this should fix it.

+7
source

ASP.NET Website Paths

To overcome these shortcomings, ASP.NET includes a web application root operator (~), which you can use when specifying the path to the management server. ASP.NET resolves the ~ operator to the root of the current expression. You can use the ~ operator in combination with folders to specify a path based on the current root.

...

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

+3
source

Source: https://habr.com/ru/post/1299669/


All Articles