What is the difference between Relative and Absolute paths in Aspnet MVC? - asp.net

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

Related

Symfony 2 image with relative path display

I don't understand, why relative path for my image url don't work.
My image located at /web/bundles/mybundle/upload/image.jpg
In my template:
<img src = "bundles/mybundle/upload/image.jpg"/>
Application entry point - app.php is located in /web folder, on the same level as bundles folder and I suppose to see my image, when I render template.
But it seems, that image path is incorrect, because I do not see my image.
Only absolute path, with host, renders it correctly.
Can I use relative path or should proceed with absolute?
you should use the embeded asset package system, that resolves correctly the assets relative paths:
<img src="{{ asset('bundles/mybundle/upload/image.jpg') }}" />
Note that adding a leading slah ("/") has an impact.

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

How to set a relative web path in a HTML page

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.

CSS background-image won't show with short URL

I've done my searching and the topics haven't been of help.
I'm trying to have the background image of my header repeat across the X axis of the header div.
When I make CSS with a long URL such as
background-image:url('http://site.com/images/logo.png'); everything works fine
When I try to shorten the CSS to something such as ~/images/ or even having the CSS and site file already in the root folder and using /images/ I get nothing
background-image:url('~/images/logo.png')
background-image:url('/images/logo.png')
This is possibly because you're not shortening your URLs appropriately.
Assuming an absolute path of:
url('www.example.com/images/imageName.png');
A root-relative URL would be:
url('/images/imageName.png');
And a relative path (assuming your CSS file is in www.example.com/css/cssStylesheet.css) would be:
url('../images/imageName.png'); /* parent directory, then the images directory */
The ~ prefixed url format is unknown to me, though I suspect it's an ASP, or .NET, form? Though I'm unable to advise on that.
Questions that might be of use to you:
How do I turn a relative URL into a full URL?
Using relative URL in CSS file, what location is it relative to?
Absolute urls, relative urls, and...?
A URL containing "~" is something that's specific to ASP.NET, it's processed server-side and transformed into a "proper" URL such as http://mysite/my_virtual_directory/images/logo.png. Web Browsers don't have any way to do this as they don't know to what "~" refers.
You need to ensure that the URLs you use in your CSS file are "understandable" by the browser, so either have them "fully qualified" (http://mysite/my_virtual_directory/images/logo.png) or starting from the "beginning" (/my_virtual_directory/images/logo.png).

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