Image URL?

R

rockdale

I believe this must be a very sample question.

I have a image dir under myWebsite (myWebSite is the root dir of my web
appl)

I have an ascx file under myWebsite/userControls

in the c# of ascx file, I load a image file using MapPath.

imgSort.ImageUrl = Server.MapPath("Images/sort_asc.gif");

If I check the source from IE

I found that the imgSort's image is c:\myWebsite\sort_asc.gif

It points to my client local c driver and the img does not show up.

Of course if I access myWebsite from server, I got the correct image,
'cause that is the C driver on server.

I tried to use relative path, but got no luck.

How should I point to the image url?

Thanks a lot
-Rockdale
 
M

Mark Fitzpatrick

MapPath is only good when you're trying to access the files on the server in
code, for example, when opening an Excel spreadsheet to edit, or to write an
image out. It won't do anything for the client.

You can take the application path, and add the path to the file with it like
so:

imgSort.ImageUrl = Request.ApplicationPath + "/Images/sort_asc.gif";

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
R

rockdale

Thanks for your reply.

It is works, partially, I would say.
On my development machine, I have this dir structure

c:\Inetpub\wwwroot\MYWEBSITE

On my production machine, I did not put MYWEBSITE under Inetput. I put
it under c:

c:\MYWEBSITE

So my user control's dir is
Dev Server c:\Inetpub\wwwroot\MYWEBSITE\userControls and
Production Server c:\MYWEBSITE\userControls

my image's dir is

Dev Server c:\Inetpub\wwwroot\MYWEBSITE\Images
Production Server c:\MYWEBSITE\Images

Trying to understand the dir completely, I tried the following cases. I
access MYWEBSITE from developement server itself. from a client other
than production server. and from production server itself. And I got
the following result

my understanding is, absolute dir does not work if the production
website is under a different dir than it is on dev server. If it works
on dev server, then it won't work on production server, vise versa.

finally, I go for relative path, I am not sure is it the best solution,
but it works on both dev server and production server.

I really hoped that Request.ApplicationPath + "/Images/sort_asc.gif";
is smart enough to know the application path, but it is not.

Maybe I am missing something?

The folowing shows the ImageUrl used in code behind, the HTML url that
generates and if it works.

imgSort.ImageUrl = "Images/sort_desc.gif"
Dev Server
http://localhost/MYWEBSITE/Controls/Images/sort_desc.gif doesn't work
Client - Production Server
http://WEB_SERVER_NAME/Images/sort_asc.gif Works
Production Server
http://WEB_SERVER_NAME/Images/sort_asc.gif Works

imgSort.ImageUrl = Request.ApplicationPath + "Images/sort_desc.gif";
Dev Server
http://localhost/MYWEBSITEImages/sort_asc.gif doesn't work
Client- Production Server
http://WEB_SERVER_NAME/Images/sort_asc.gif works
Production Server
http://WEB_SERVER_NAME/Images/sort_asc.gif works

imgSort.ImageUrl = Request.ApplicationPath + "/Images/sort_asc.gif";
Dev Server
http://localhost/MYWEBSITE/Images/sort_desc.gif works
Client-Productiion Server
http://images/sort_desc.gif doesn't work
Production Server
http://images/sort_asc.gif doesn't work


imgSort.ImageUrl = "../Images/sort_asc.gif";
DEV SERVER
http://localhost/MYWEBSITE/Images/sort_asc.gif WORKS
Production Server
http://WEB_SERVER_NAME/Images/sort_asc.gif WORKS
Client-Production Server
http://WEB_SERVER_NAME/Images/sort_asc.gif WORKS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top