Reputation: 65870
string serverPath = HttpContext.Current.Server.MapPath("~/web" + event.Image_Url);
var isFileExist = File.Exists(serverPath);
The value of event.Image_Url = /Resources/images/event-images/e1ae04a2-e63f-4831-a5ee-2f0d2713f8a2.png
But it always gives false
even though the file exist on the physical path.The physical path which it comes from above operation as shown below.
serverPath value = D:\Freelance Work\Trunck\Api\web\Resources\images\event-images\e1ae04a2-e63f-4831-a5ee-2f0d2713f8a2.png
But actually I need to go to the web
folder.But it automatically gets the Api
folder as shown above.How to avoid it ? Why it takes the Api
folder ? Any help would be highly appreciated.
Note : I have noticed that the web api
project is also running on local host.May be that is the reason for it.But how can I tell it to get the virtual path from the web
project ?
Folder structure within Trunck as follows.All are in same level.
Trunck --> API
--> Web
--> BLL
Upvotes: 0
Views: 791
Reputation: 156978
The problem was the /
at the start of the event.Image_Url
field. That caused the last path in the string to be taken as a absolute path.
string serverPath = Path.Combine
( HttpContext.Current.Server.MapPath("~")
, @"..\web\"
, @event.Image_Url.TrimStart('/').Replace('/', '\\')
);
Upvotes: 3
Reputation: 8184
I think you need to try string path= Server.MapPath("~/web"), serverPath =Path.Combine(path,event.Image_Url)
instead of string serverPath = HttpContext.Current.Server.MapPath("~/web" + event.Image_Url);
Upvotes: 0