开发者

ASP.NET Get physical filepath from URL

Is there a way to get the physical filepath from an ASP.NET's U开发者_如何学GoRL?

Scenerio: I have an app that is on two severs, but it will now be on lots more, and each server puts it in a different physical file path. Right now I'm doing this:

//for server 1
if (Request.Url.GetLeftPart(UriPartial.Path).Contains(".com"))
 { Application["StoreFilesPath"] = "E:\\Data\\rootsite\\f1\\appsite\\Upload\\"; }

//for server 2
if (Request.Url.GetLeftPart(UriPartial.Path).Contains(".net"))
 { Application["StoreFilesPath"] = "E:\\Web\\rootsite2\\f34\\abc\\ghi\\appsite\\Upload\\"; }

But what I need to do is something like this:

//for all servers
Application["StoreFilesPath"] = getPhysicalFilePath() +"\\Upload\\";

How can I do it?


You can use HttpServerUtility.MapPath on the server side in order to get the physical path of a file, then return it in the Application or Session object, similarly to what you are doing now.

As for the physical path of a URL - there might not be one, as URLs can be re-written.


This Server.MapPath ( "/" ); or this HttpContext.Current.Server.MapPath ( "/" ); should give you what you need.


This is now:

HostingEnvironment.MapPath("/");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜