In c# code, I need to write src for an image. Does anyone know how to get website root path in c#? My folder structure is UI/Image I found that when I use
string rootpath=Page.Request.ApplicationPath;
If run the application in debug model, it works. But if run it by typing url directly, it won't show image. The property of the image is http://image/turnon.bmp which should be http://localhost/image/turnon.bmp
Any idea?
Simple use the ~ sign
as the ~ represents the root of your app.
so yourimage url will be
<img src='<%= Server.MapPath("~/images/1.jpg") ' />
An easy way is use MapPath
with the ~
wildcard:
string imagePath = MapPath("~/image/turnon.bmp");
As Dan Csharpster stated in the comments, since the Server
object which exposes the MapPath
method is not directly available in a class library the command should be
string imagePath = HttpContext.Current.Server.MapPath("~/image/turnon.bmp");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With