ASP.NET Get physical filepath from URL

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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.

Method 2

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

Method 3

This is now:

HostingEnvironment.MapPath("/");


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x