471,066 Members | 1,159 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

Image URL?

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

Jun 20 '06 #1
2 2596
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


"rockdale" <ro************@gmail.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...
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

Jun 20 '06 #2
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/.../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

Mark Fitzpatrick wrote:
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


"rockdale" <ro************@gmail.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...
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


Jun 20 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Pierre Tremblay | last post: by
3 posts views Thread by dave | last post: by
8 posts views Thread by Jef Driesen | last post: by
6 posts views Thread by QuasiChameleon | last post: by
15 posts views Thread by Anand Ganesh | last post: by
7 posts views Thread by lgbjr | last post: by
6 posts views Thread by comp.lang.php | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.