Server.MapPath - Not allowed to load local resource error
13,842
Solution 1
You can try this
string path = Server.MapPath("~/Uploads/User_" + userId + "/");
Map path need Virtual path so "~/" will surely fix it.
Solution 2
For me removing Server.MapPath worked.. I use tild ('~') sign with the url and it worked perfectly.
string path = "~/Uploads/User_" + userId + "/";
Author by
1110
Updated on June 04, 2022Comments
-
1110 almost 2 years
I use
Server.MapPath
to access some images on server. And I got the right path but can't open it. The error I got is:Not allowed to load local resource: d:/Project/MyProject/WebUI/Uploads/User_18234/leopard.jpg
I suppose that this will work on server. But I need it on localhost to test. Is there any solution for this?
... userId = GetUserId(); string path = Server.MapPath("Uploads/User_" + userId + "/"); model.Name = path + model.Name + "_Original." + model.Extension; ...
-
1110 over 12 yearsNo it doesn't work like that. But it works when I use "../../../../Uploads/User...".
-
Ankit Patial over 12 yearsyes you are correct, it means the virtual path you need is "~/../../../Uploads/User...". Path must be with respect to project root.
-
1110 over 12 yearsThis means that when I write "~/../../../Something" this actually is relative path. Am I right?
-
Ankit Patial over 12 yearsif possible do use the Absolute path e.g. "~/<dir>/<dir>/<file>" better approach than the relative path e.g "../../<file>". Reason is that "../" varies w.r.t caller(page) depth