How to set a relative web path in a HTML page - asp.net

I am building a website. I have many folders. The highest layer is called FOLDER1. I have two folders in FOLDER1. They are FOLDER2A, and FOLDER2B. Inside FOLDER2B, there is a html file. I need to use a picture "DOG" in FOLDER1 in that html. If I don't want to use absolute path like http://abc.com/FOLDER1/DOG.jpg as a path, how I can use a relative path like /Folder1/DOG.jpg <=== I know that this is wrong
Thanks

../DOG.jpg unless I'm missing something here. .. means up one folder. ../../ means up two, etc. To access an image in FOLDER2A from FOLDER2B ../FOLDER2A/CAT.jpg

You can refer to files in the parent directory using ../, i.e. ../DOG.jpg will refer to DOG.jpg in FOLDER1 if referred to from inside FOLDER2A or FOLDER2B.

Related

Finding the right path of an image

I've a web site on www.open-guide.com and I'm trying to put a background-image for each list item of my General Information form (Why choose us?), however I can't seem to find the right path (either relative or absolute) for the images, which I've in a folder (tour_icons) in my desktop.
I tried:
li {
background-image: url(../tour_icons/imagename.jpg);
}
But it is not finding it. What am I doing wrong?
You need to copy "tour_icons" folder into the hosted application path root, otherwise it wont find the correct file path. use / only as a prefix to start the path it will consider the root of application and define path asa string format.
background-image: url('/tour_icons/imagename.jpg');
It should be:
background-image: url("../tour_icons/imagename.jpg");
You should enclose the path in quotes " or '.
Also ../ Means parent directory of current css file. if you want to access child directory use : / instead of ../.

What is the difference between Relative and Absolute paths in Aspnet MVC?

This relative and absolute paths always confuse me. i want to know how and where to use them in Asp Net MVC.
For Ex- If i want to use a img tag-
img src="#Url.Content("~/Content/themes/base/images/logo.png")" alt="Koiak Basic Site" />
img src="/Content/themes/base/images/logo.png" alt="Koiak Basic Site"/>
Kindly explain the difference between both of them
Absolute Path:
An absolute URL path. An absolute URL path is useful if you are referencing resources in another location, such as an external Web site.
<img src="http://www.contoso.com/MyApplication/Images/SampleImage.jpg" />
Relative Path:
A site-root relative path, which is resolved against the site root. Site-root relative paths are useful if you keep resources that are used throughout the site, such as images or client script files, in a folder that is located under the Web site root.
The following example path assumes that an Images folder is located under the Web site root.
<img src="/Images/SampleImage.jpg" />
For More Refer:
http://msdn.microsoft.com/en-us/library/ms178116.aspx
Coming to your Question:
<img src="#Url.Content("~/Content/themes/base/images/logo.png")" alt="Koiak Basic Site" />
Here because of using "~".It adds "server" path(i.e; your application path)" to your url. That means it takes img src as "yourapplicationPath/Content/themes/base/images/logo.png"
<img src="/Content/themes/base/images/logo.png" alt="Koiak Basic Site"/>
Here it takes as it is. i.e;"/Content/themes/base/images/logo.png"
For more refer this:
why use #Url.Content
http://digitalzoomstudio.net/2012/04/01/what-is-the-difference-between-absolute-and-relative-paths-urls/
What is the difference between / and ~/ relative paths?
Absolute Path
In terms of directory
When we refer to a location from root like C:\Documents\MyFolder, it is absolute path.
In terms of URL
Absolute paths are called that because they refer to the very specific location, including the domain name. The absolute path to a web element is also often referred to as the URL. For example, the absolute path to this is:
http://www.stackoverflow.com/posts/21670682
Relative path
In terms of directory
When we refer to a location relative where we currently are, it is called relative path. For example, say currently you are at Documents folder in C:\Documents, to refer to MyFolder you have two choices: Absolute (C:\Documents\MyFolder) or relative (\MyFolder).
In terms of directory
Relative paths change depending upon the page the links are on. There are several rules to creating a link using the relative path:
links in the same directory as the current page have no path information listed
filename
sub-directories are listed without any preceding slashes
weekly/filename
links up one directory are listed as
../filename

Background not working for a div as it should

I have a strange problem with paths, this one works (on Windows):
<div style="background:url('folder1/image.gif')...
But this one won't work (no image shows up):
<div style="background:url('/folder1/image.gif')...
Still this page says exactly the opposite (not the first but the second version should work): Background not working for a div
Anybody knowing what the reason might be?
The first url is relative to the folder in server what your HTML is used to render the page.
Example, if you get:
www.mywebsite.com/index.html
it will look into: (example 2)
www.mywebsite.com/folder1/image.gif
but if you are in another folder like:
www.mywebsite.com/subfolder/index.html
It will look in:
www.mywebsite.com/subfolder/folder1/image.gif
If you use a '/' in the beggin, the path isn't more relative, it always look in the root website, like exemple 2 no matter where your html is located.
Depends on where your image and html file are located.
'folder1/image.gif' will search for a folder1 that is located in the same path as your html file (relative path).
'/folder1/image.gif' will search for a folder1 starting from the base location of your server (absolute path).
The first is a relative path, the second is an absolute path
Relative paths show the file path from the calling context. So if your html file is /source/website/test.html, a relative path of css/test.css will point to a file in /source/website/css/test.css
Absolute paths show relate to the whole path, so /css/test.css tries to find a file at the location /css/test.css
Most modern browsers allow you to inspect elements on a web page. On chrome (or other modern browser) open the console and look for errors, if the image url is wrong, the console will indicate it as an error, moreover you will get the broken-image icon if you have specified the wrong url

ASP.NET: question regarding reference paths

These are the three ways in which the same image has been referenced to in my project in 2-3 different files:
url(<% =QDAAB.Constants.SiteURL %>images/Docs/Plan/myImage.jpg)
url(~/images/Docs/Plan/myImage.jpg);
url(../images/Docs/Plan/myImage.jpg);
I am a bit confused about this relative, absolute path thingy. How do I refer to this image in such a way that no matter in what folder the image is located it is always displayed and never lost. Which of the above three references ways is right to achieve what I want? Or is there another way too to refer to an image?
Hope the question is clear.
I am beginner to .NET and have just begun trying out stuff.
Also what exactly do the above code lines mean? Like, what's this Constants.URL? What are "~" and ".." doing?
Here's where the image is being used in one of the pages. When I changed it to "../.." the image got displayed, earlier it wasn't.
<td align="center" style="background-image: url(../../images/Docs/Plans/myImage.jpg);
None of these will 'magically' find your image no matter in what folder the image is located. You will always have to give it some part of the path to the image in your links to have that image rendered.
However, out of these three, I would recommend the second line url(~/images/Docs/Plan/myImage.jpg); as the best way to accomplish what you need to do.
The '~' in .NET is a special character for the framework that starts your path at the root of your site. It essentially says, no matter what directory the request came from within the site, go to the root and start your path from there.
The '../' is standard HTML syntax meaning go up one directory and then start looking for your path.
So for instance, let's say your site is located at www.domain.com. And here is your folder structure
Images
Docs
Plan
CSS
Admin
SomeFolder
SomeFolderSubFolder
Default.aspx (your default document when you get to your site)
Examples
If you are in default.aspx, then the hand-coded URL to your images would be (/Images/Docs/Plan/myImage.jpg) as you are at the root of your site at this point.
If you are in SomeFolder, then the hand-coded URL to your images would be (../Images/Docs/Plan/myImage.jpg) since you have to go up 1 directory to get to the root of your site where the Images directory is located.
If you are in SomeFolderSubFolder, then the hand-coded URL to your images would be (../../Images/Docs/Plan/myImage.jpg) - as you would need to go up 2 directories to get to root of your site where your images folder is.
Now, by using the url(~/images/Docs/Plan/myImage.jpg); method, you do not have to worry about knowing how many directories deep you are in your site, it will always start looking from the site root. Each one of those examples above replaced with this line of code will always render out the image correctly.
EDIT
Ok, so you are trying this with in-line styles. You might/should be able to do this:
<td align="center" style="background-image: url(<%= Server.MapPath("~/images/Docs/Plans/myImage.jpg") %>);></td>
Or, there is nothing wrong with using the ../ method, you just need to keep in mind where you are at in your folder structure. Lastly, if you were to declare this style in your stylesheet instead of inline, it doesn't matter where the images are at in relation to the page, just where they are at in relation to the stylesheet FYI.
<td align="center" class="tdWithImages"></td>
styles.css
#tdWithImages{background-image: url(../Images/Docs/Plan/myImage.jpg);}

Path to css and images

For example I have site http://localhost/site
In IIS I set that 404 error causes redirection to default.aspx
If I type something like http://localhost/site/nodirectory , (there are no such folder) all works perfectly.
But if I only add slah at end http://localhost/site/nodirectory/, page can't display css and images.
Images and css are located in their own folder. I tried different paths: "gfx/logo.gif", "/gfx/logo.gif"
Does anyone have some ideas about that?
If your css and images are relative paths, say ResolveClientUrl("~/gfx/logo.gif") this renders to the client as src="gfx/logo.gif", which the browser with a slash thinks is /nodirectory/gfx/logo.gif instead of just /gfx/logo.gif.
To resolve this, don't use .ResolveClientUrl(), use .ResolveUrl(), this will make the src render src="/gfx/logo.gif" The beginning / makes it definitive, it's that path from the root of the domain.
You'll see this same hebavior if you're doing paths that start with ../ or gfx/ yourself...make them relative to the application base so there's no chance of confusion.
There are a couple of options...
1)
In your HTML page, make the path to CSS and scripts relative...
"/scripts/myscript.js"
Where the scripts folder is the first folder after the root folder
2)
You can add the base tag to your page, which means ALL page resources will be treated as relative to the root location you specify...
<base href="http://www.mysite.com">
More info about these two options.
If you can, option 1 is perhaps a bit cleaner. You know explicitly the resources that you are affecting. Using the base tag will affect ALL relative paths on your page. Images, Links, Scripts, CSS et al. The second option works best if you developed your 404 page assuming it would be in the root folder, but it could actually be referenced from any non-existent directory. You just put your root address in the base tag and it will all behave exactly as you expect.
With either option, the images can be relative to the location of your CSS file.

Resources