ASP.NET Get physical filepath from URL

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 16:33:58

问题


Is there a way to get the physical filepath from an ASP.NET's URL?

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?


回答1:


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.




回答2:


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




回答3:


This is now:

HostingEnvironment.MapPath("/");


来源:https://stackoverflow.com/questions/3833680/asp-net-get-physical-filepath-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!