Relative path from site root - asp.net

I feel like a nub for asking this, but I can't figure it out.. I've found several posts (here's one) saying that to use a relative path from the root of your site, start the path with /, ex:
<img src="/images/img1.jpg" />
My file hier. looks like
-root
-images
-css
-index.aspx
-subFolder
-test.aspx
Now when I use the src path as shown above, it doesn't work in either index.aspx or test.aspx, but when I remove the /, it works for index.aspx. In test.aspx, I used ../images/img1.jpg and it works. What gives? Why is my example above not working?

Your site is probably in a virtual directory, so the "/" refers to the actual web root as IIS (or whatever web server) sees it - maybe the folder above your 'root' folder
.

The relative paths work because they are traversing the directory based on the location, so for index.aspx it needs to go into images folder and get the img1.jpg, for test.aspx it needs to go up one level .. then into the images folder and get the img1.jpg.
Absolute paths are based off of were the application is installed from based on IIS settings. If you are just testing this from that folder your absolute path needs to include the root folder. /root/images/img1.jpg and then it will work from both test and index with the same absolute path.

Related

What is the difference between / and ~/ relative paths?

I thought that both types would bring you to the root folder, but apparently, they work differently, once you do a URL rewrite.
For instance, I normally use / which I know will bring you to the root folder and it does when a URL has been rewritten.
When someone else tried to use ~/ after a URL has been rewritten, then the path fails to find the file. Why is that?
/ will take you back to the root of your website.
~/ will take you to the home folder of your application on the website.
If your application is in a folder called myApp, for example, so the URL looks like this
http://www.YourSite.com/myApp/
and in your application you use ~/Scripts/jquery.js then the path referenced will be
http://www.YourSite.com/myApp/Scripts/jquery.js
whereas just using / would send you all the way back to the root of the website
http://www.YourSite.com/Scripts/jquery.js

FileHelpers file access

I am trying to get my head over FileHelpers library and have main critical problem. When I try to implement it in my web app, or even use online demo I end up with "FileNotFoundException". The chosen file is being looked for on my C: drive. How can I make the FileHelpers code to access relative path to my application instead of absolute one?
Regards,
Bartosz
Use the Server.MapPath() method to map a relative path (based on current directory or web-site root) to an absolute accessible path.
For example, if yourfile.txt is placed inside App_Data folder of your web-site then you can write:
Customer[] customers =
(Customer[])engine.ReadFile(Server.MapPath("~/App_Data/yourfile.txt"));
The tilde character represents the root of your web-site, if you specify a relative path then it'll be resolved as relative to the directory where your ASP.NET page resides.

Different between link prefix "~/" and "/"

Sometime we need to put / or ~/ as the suffix in order to make refer to the root
<script type="text/javascript" src="/scripts/jquery-1.5.1.min.js"></script>
<link href="~/Styles/MasterPage.css" rel="stylesheet" type="text/css" />
This is what i know from Phill
~/ is not valid unless you have a runat attribute on your control/element. '/' by itself specifies to the browser to look from the root directory, so if you link to '/css/main.css' from www.mysite.com/product/view.aspx it will look for the css file in www.mysite.com/css/main.css. When you use '~/' with runat-server, it will work out the path to the directory at runtime, so in the same example with '~/css/main.css' the rendered url will look like '../css/main.css' because it needs to drop the directory back 1 before finding the directory 'css'. (hope that makes sense) – Phill
How to include jquery in masterpage header without path problems?
but as i has tried, even i use / firefox still refer to ../../ instead of static path.
also, I don't know it clearly how to use it correctly , which one use on which situation.
~/ refers to virtual directory application root where as "/" refers to site roots eg if you have site that in http://www.example.com/ and which has http://www.example.com/foo where folder "foo" configured as virtual directory application in IIS a page in http://www.example.com/foo/foo.aspx will resolve ~/ as relative to virtual directory application root directory which is http://www.example.com/foo but "/" will still resolve to site root which is http://www.example.com/
The / at the beginning of a relative URL backs up to the root of the hostname.
The prefix ~/ in a relative URL can only be interpreted by the ASP.NET process, which is why it only works for runat=server. It is translated upon output to refer to the root URL of the ASP.NET application. Many times, this is the same as the root of the hostname, but it will differ if the ASP.NET application that the code is running in is a virtual directory in IIS rather than its own site.
If you only need to drop one directory level back, you can use "../" as the prefix rather than "/" or "~/".

Can you specify the differences ./,../,~/ in ASP.NET?

What are the differences between ./, ../, and ~/ for specifying an image in my web application?
Like current directory, root, parent directory, etc.
"./" //the current directory
"../" //the parent directory
"/" //the site root directory
"~/" //virtual root Web path
ASP.NET Web Project Paths is a very good article in MSDN regarding paths in ASP.NET with good examples.
Web Pathing:
./ means this folder, equivalent to nothing on the front
ex: ./image.jpg and image.jpg are the same thing
/ the web application root folder
ex: /Test/image.jpg, go to the application root, find folder Test, find image.jpg
../ means up one folder then go to the folder after the slash...
ex: ../Test/image.jpg, go up one folder from the current folder your in, go into folder Test, and find image.jpg.
Physical Pathing:
~/ means the application physical root folder. This will not work if you are using it directly in the webpage (ex: <img src="~/images/image.jpg" />). You can use ResolveUrl to get the web path to a file. I have used this for referencing JavaScript libraries in a master page. I've also seen it used in some custom uploaders.

What IIS 6.0 setting determines how a path is resolved?

I have a website that is deployed between 3 different environments - Dev, Stage, and Prod. For Stage and Prod, the site can resolve local paths to images with just the base url to the file, such as /SiteImages/banner.png. However, on the Dev server I have to hard code the full URL of the image path for the image to be resolved, such as http://server/folder/SiteImages/banner.png. Is there a setting I can flip to make the Dev server behave in the same manner as the other 2? I am using IIS 6.0 on a Win 2003 server.
There are usually three kinds of URIs that you can code in a website as far as I know.
Absolute: http://yoursite.com/somehing.jpg
This url includes the http:// and is
the full path to a resouce.
Root Relative:
/something/something.jpg
(In ASP.net
server-side only, you would use
'~/something/something.jpg')
This path is relative to the root of
your site.
File relative:
../something/something.jpg
This path
starts at the location of the file
that includes the URI. In this case,
it just back one directory (..) and
then goes back into the something
directory to look for something.jpg
What does the URL to the dev home page look like? Is it something like http://server/mydevsite/? If so, it sounds like you need to set up a virtual host.
Edit Just to clarify the above, say your prod and stage sites can be simply referenced as http://stagesite/ and http://prodsite/, if you use a path such as /images/myimage.jpg it assumes that the images folder are sitting in the root. In those two instances, no problem, images will display correctly. However, let's say your dev server is like the example I listed above. If your images are references as /images/myimage.jpg, instead of the server looking at http://server/mydevsite/images/myimage.jpg, it will instead look at http://server/images/myimage.jpg. If no images folder with the requested image exist in that server's root, you'll get an error.

Resources