Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get website root path in c#?

Tags:

c#

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?

like image 310
Steven Zack Avatar asked Sep 20 '11 16:09

Steven Zack


2 Answers

Simple use the ~ sign

as the ~ represents the root of your app.

so yourimage url will be

<img    src='<%= Server.MapPath("~/images/1.jpg") '  />
like image 23
Royi Namir Avatar answered Sep 19 '22 10:09

Royi Namir


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");
like image 135
Erick Petrucelli Avatar answered Sep 23 '22 10:09

Erick Petrucelli