asp.net debugging issue - website paths
My project is laid out such that there is the .sln and beneat开发者_运维知识库h that a bunch of folders - all of which have a class library project in them. Also in that folder list, is a folder that contains the website (called Website)
In the website I have various folders for images, master pages and so on.
In the master page, I am loading an image and reference it thus: src="/images/logo_blue.jpg", for that is where the picture is.
However, when I run the code up in the debugger, the master page isn't picking up any of the css, or js files, nor the images - I and feel it is because the website path in IE is given as:
http://localhost:61249/Website/Default.aspx
and if I right click on the x where the image should be, it is looking in http://localhost:61249/images/logo_blue.jpg" but it seems that the image can be found in http://localhost:61249/website/images/logo_blue.jpg"
Why is the debugger inserting the spurious "website" into the path? Why isn't the root of the actual website the website folder - it looks like the root of the webste is actually the solution folder.
I imagine that I am just doing something very obvious....
Check in your web project's properties page on the Web tab. There should be a setting for Virtual Path - you want that to be just "/".
You will also probably end up having to add a '~' in front of the path, src="~/images/logo_blue.jpg". This tells the page to look for this path relative to the site root. Master pages are somewhat notorious for losing their references when they are consumed by a child that is in a different folder.
Depending on where exactly this is being referenced you may also have to wrap the url in a call to ResolveClientUrl. In the code behind the syntax would be:
src=Page.ResolveClientUrl("~/images/logo_blue.jpg")
If you are using a standard HTML image tag it would look like this:
<img src='<%= Page.ResolveClientUrl("~/images/logo_blue.jpg") %>' />
Or if you are using a asp image control your markup would be:
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Page.ResolveClientUrl("~/images/logo_blue.jpg") %>' />
but in this case you must also call the control's databind method at some point so that the URL is resolved.
精彩评论