Server.MapPath not returning expected path - asp-classic

I've been handed alegacy (Classic ASP), red headed step child who has been beaten, shot and otherwise maimed by a number of people before my time, application and for the life of me I can't figure out how it's actually working on production (I even got a copy from production just in case the files weren't up to date in source control).
I have code that does the following
tmpDefaultXSLFile="xsl/SomeValue"
...
objXsl.load(Server.MapPath(tmpDefaultXSLFile & ".xsl"))
However, Server.MapPath seems not to map the file to the expected location (i.e."\asp_file_directory\xsl\SomeValue.xsl" and the modification required is to the xsl file.
Is there ANY way that the system could be fooled into have Server.MapPath map somewhere else?

Looks like virtual directory on IIS is causing this. Check the URL and compare it to the directory structure within IIS including the virtual.

server.mappath(Path) specifies the relative or virtual path to map to a physical directory.
If Path starts with either a forward (/) or backward slash (\), the MapPath method returns a path as if Path is a full virtual path.
If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the .asp file being processed.

Related

File Path and Root issues

So I have my the path to my website code as follows:
C:/folder1/folder2/folder3/my published website code from VS2012 - on my website I get an attachment and I want to save it to the following path C:/folder4
when I try the following code: file.SaveAs(Server.MapPath("../../folder4/") + filename); it says that I am going past the root. Can someone explain to me what is going on and if and how I can solve this issue?
Server.MapPath() is used to get the path in relation to the server root. Since your trying to save it outside the server virtual directory, you could probably just hardcode the file.
file.SaveAs(#"C:/folder4/" + filename);
It might not work depending on your IIS worker pool permissions.
file.SaveAs(Server.MapPath("folder4/") + filename);
Because I cannot see your folders structure I would reccomend setting a breakpoint after Server.MapPath() to see the full URI Path to determin your next steps since it says you are past root you may have one to many "../" before your string.
As per the documentation for HttpServerUtility.MapPath:
you cannot specify a path outside of the Web application
which is exactly what you are trying to do. If you interpret "the root" to be the root folder of your application, that is even what the error message is telling you.
Either
use an absolute path or
store your data beneath the application folder
use MapPath("~/") to get the current directory and build a relative path from that (in essence, you just move the "../.." outside the call to MapPath)
I would probably recommend going with 2. as it will give less headaches wrt. permissions and multiple sites hosted on the same server.
Server.MapPath(...) tries to return a physical ("real") directory for the virtual or relative path you give it. And since a virtual directory can't be located "over" the root in that sense, what you're trying to do makes no sense. You can go from domain.com/somefolder to domain.com/, but you can't really go any farther back.
You could instead use Environment.CurrentDirectoryas the starting point to find your folder, and apart from that just use SaveAs(..) as you're already doing.

Referencing a virtual directory in C#

I have a webserver setup in iis called WebServer, in location ex. C:\inetpub\WebServer\ I have created a virtual directory call it virtualdir where the physical path is outside of the path of my webserver for example the Virtual directory points at C:\inetput\virtualdir, everything works fine and I can access files going to www.myserver.com/virtualdir/test.txt, however I was wondering if from my default.aspx page I can reference the virtual directory as part of my project like so
File.Exists("~/virtualdir/test.txt)
or do I have to reference the complete path of the virtual directory like so
File.Exists("C:/inetpub/virtualdir/test.txt")
Thank you,
You need to convert a relative, web based path containting the "~" to a path that file.exists can deal with. in order to do that call server map path to convert one to the other. See
http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath(v=vs.100).aspx

FileHelpers file access

I am trying to get my head over FileHelpers library and have main critical problem. When I try to implement it in my web app, or even use online demo I end up with "FileNotFoundException". The chosen file is being looked for on my C: drive. How can I make the FileHelpers code to access relative path to my application instead of absolute one?
Regards,
Bartosz
Use the Server.MapPath() method to map a relative path (based on current directory or web-site root) to an absolute accessible path.
For example, if yourfile.txt is placed inside App_Data folder of your web-site then you can write:
Customer[] customers =
(Customer[])engine.ReadFile(Server.MapPath("~/App_Data/yourfile.txt"));
The tilde character represents the root of your web-site, if you specify a relative path then it'll be resolved as relative to the directory where your ASP.NET page resides.

get path to an included file on Classic ASP?

I have a file that is being included, in that file there's
Server.MapPath('../_data") which doesn't work
since that file included is not in the same Server.MapPath as the file executed.
any Idea of how I can get the included file's path?
To clarify the situation, I added a picture
As you can see, I'm including a file from one site to the other
(no other choice there), so that the server.mappath is intended to be different, though
the result is that on the included file I get the mappath of the executed file.
You should better use relative paths.
Relative paths start with / which means start from the root of the site..

An invalid '/' or '\' was found in the Path parameter for the MapPath method

Like the title says, the error I get is the following:
An invalid '/' or '\' was found in the Path parameter for the MapPath method.
I'm using ASP classic and basically trying to access a folder on one of our servers. I Googled around and judging from what most people say, I should take off the \ prefix from my server path. But when I do that, the code cannot find the server. The code I'm using is:
Dim fileSystem
Set fileSystem = Server.CreateObject("Scripting.FileSystemObject")
fileSystem.GetFolder(Server.MapPath("\\servername"))
So my question is, how can I use the FileSystemObject to access a servers' files and folders?
Thank you.
First, Server.MapPath is never going to like "\\servername" because that's a UNC path to a windows server, not a portion of a URL. If you want to get that, this might work:
fileSystem.GetDrive("\\servername")
Second, if you are really looking for a URL, it should be an absolute URL starting with a / or a relative URL starting without a / and that will be relative to the current .ASP page. If you aren't processing a request, you'll only be able to use an absolute URL.

Resources