Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

significance of tilde sign

string path = context.Server.MapPath("~/Temp");

or

string path = context.Server.MapPath("/Temp");

are same ?

I know that '~' represents root but want to know diff bw ~/folder and /folder

like image 598
Ankush Jain Avatar asked Oct 20 '25 06:10

Ankush Jain


1 Answers

Absolute and relative path references in a server control have the following disadvantages:

  • Absolute paths are not portable between applications. If you move the application that the absolute path points to, the links will break.

  • Relative paths in the style of client elements can be difficult to maintain if you move resources or pages to different folders.

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.

See http://msdn.microsoft.com/en-us/library/ms178116(v=vs.100).aspx

~/ resolves to the application root.

/ resolves to the site root.

When a server resource (like a control or view) is rendered, ~/ paths are resolved to site root paths based on the structure and context of the application (since ~/ is meaningless to a web browser).

To simplify, application root (~/) is almost always the correct choice in ASP.Net applications (both web forms and MVC).

like image 116
Tim M. Avatar answered Oct 25 '25 07:10

Tim M.