Server Root and MapPath() - c#

I have a file structure set up like this:
ServerRoot
applicationRoot
filePage.aspx
files
chart.png
My application page called filePage.aspx uses another app to custom build charts. I need them saved in files folder. This is how our client's production server is set up and I cannot change this.
I do a _page.Server.MapPath("/files") but it gives me a InvalidOperationException and states Failed to map the path '/files'.
UPDATE:
So it has to be set up this way MapPath("/"). My local asp.net server can't handle the MapPath that way, but our IIS development box has no problem with it and it works fine. Interesting.
How do I get it to save to files?

I believe it's a security violation to go outside the directory structure of the virtual directory in asp.net 2.0 and up. You'll need to make a virtual directory to the directory and use that.

Use
Server.MapPath("~/files")
The ~ represents the root of the web application so the folder returned will be correct no matter which subdirectory you are in.

Try
_page.Server.MapPath("files")

_page.Server.MapPath() will attempt to map from the root of the application (NOT the root of the server).
Try _page.Server.MapPath("../files").
EDIT
You may run into security issues when trying to map outside of your application root. If that is the case, you can do something like this:
Server.MapPath("~").Substring(0, Server.MapPath("~").IndexOf(VirtualPathUtility.ToAbsolute("~").Replace("/", "\"))) + "\files"
This looks rather complex, but essentially says "map my application, then remove the application root from the end and add '\files' instead".

Related

ASP physical and virtual path

This is the first time I make an asp site. This line of code is working fine on my pc but obviously to make it working on the production server I need to change the reference.
DirectoryInfo dir = new DirectoryInfo(#"C:\Users\Pink\Documents\Visual Studio 2012\Projects\ManagDoc_Framework\Test1_managDoc\Test1_managDoc\Allegati\" + recordIDcreateDir);
I have tried many sort of path combination but I am not getting it right.
I would like to find a solution that makes the code working on both pc, during development, and hosting server without having to change the code.
How should i write the path? Some help will be appreciated.
Use Server.MapPath method :
The MapPath method maps the specified relative or virtual path to the
corresponding physical directory on the server.
Additional details on W3schools.com, tutorial I followed, and where I learnt the existence of the above method.

Access to the path 'xxx' is denied (where 'xxx' is a folder)

I know this has been asked before many times, but I have browsed tens of similar questions without help.
This error is showed when my asp.net 4.0 application tries to access a folder on my local drive, doing this:
XmlTextReader confReader = new XmlTextReader (filename);
while (confReader.Read()) // <- error line
{
// do something
}
In my web.config I have <Identity Impersonate="true">. For the folder I have assigned full-access to:
everyone
IIS APPPOOL\<custom apppoolname>
NETWORK SERVICE
Debugging the application, I can put up a watch which evaluates this call right before the incriminated line:
System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
the call shows the current user is IIS APPPOOL\<custom apppoolname>.
I'm on Windows 7 64bit with Sp1. I know it's probably something stupid but I have already spent an awful amount of time on this.
Edit:
The problem was in the variable "filename", which pointed to the right folder, but for a configuration problem the filename was missing (i.e. the content was "c:\data\" instead of "c:\data\file.xml"). The Asp.net error was actually confusing, since I had all the right permissions to access that folder. DJKRAZE pointed me to the right direction.
Here is what I would suggest checking
where are you declaring filename..?
Does the file even exist..?
do you have rights to that folder..? have you tried running VS as Admin..? do you have Virtual Directory setup for the web app..sounds like you have a few things configured improperly as well but can't really tell based on the code for starters this line System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
should yield your domain\\username or compuername\\username
Glad that FileName was all it was
IIS requires the path to a folder in the server machine has rights for Anonymous user login to access.
Make sure the user Anonymous has the rights to access the folder.

file path using C#

I have a windows service which is using a method from a class library with same asp.net solution. in class library, I have a method with following line:
reader = XmlReader.Create(HttpContext.Current.Server.MapPath("~/TestDevice/Data.xml"), settings);
When control comes to this line. I get exception. I tried to debug the code and found that when service tries to access this method then HttpContext.Current.Server is null. What is alternative syntax.
I tried to access this class library method from web application and it works fine.
System.IO.Path.GetFullPath("/TestDevice/Data.xml") returns C:\\TestDevice\\Data.xml instead of the actual directory path
I want to get full path of the folder.
Please suggest solution.
http://msdn.microsoft.com/en-us/library/aa457089.aspx
string path;
path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
You will need a configuration file that can have the "root" directory set specifically. This will allow the windows service to know what directory to place files into regardless of where its executable sits and regardless of where the asp.net site is configured to run.
I don't think the ~ will work in this case, you will need to provide a relative path. Something like "../../TestDevice/Data.xml" should work.

DirectoryInfo & Path.GetDirectoryName - Exists method returns false while exists

I would like to check whether a folder exists or not if not create. I'm sure this folder exists, but for some reason I get "false" when I check with "Exists" method.
The only reason I think could be because of the W: drive?
I moved this application to production site and even there it returns false.
while I'm type in Windows explorer on my localhost and on the server "W:/Webs/ASPPages/cropper/uploads" it opens this folder. So my localhost and IIS server has W: mapping.
for test I tried to create the folder then it says can't find the path...
userFolderName = #"W:/Webs/ASPPages/cropper/uploads"
//I also tried #"W:\Webs\ASPPages\cropper\uploads\"
//I also tried #"W:\Webs\ASPPages\cropper\uploads"
//I also tried "W:\\Webs\\ASPPages\\cropper\\uploads"
DirectoryInfo dirInfo = new DirectoryInfo(userFolderName);
if (dirInfo.Exists) //returns false
var diPath = new DirectoryInfo(Path.GetDirectoryName(userFolderName));
if(diPath.Exists) //returns false
My opinion/experience is, that uploaded content should neither be part of your website nor should it be in your IIS websites directory. First it messes things up heavily when you have source control. The second problem is because of security, which is very restrictive for those IIS folders and so you end up in situations like the current one.
Now to the possible solution. Create the uploads folder somewhere else, on a separate drive or just another directory but make sure that it's not inside your IIS folders. Then go into IIS, select your website in the left tree control and set up a virtual directory pointing to that folder you just created. Now you probably don't need to do anything else, but if needed set up any additional rights you want to that new uploads folder and DirectoryInfo.Exists() will 99% work as expected now.
Good luck!
try using this in your code behind
userFolderName = Server.MapPath("/uploads");
Drive letters are mounted on a per-user or per-session (as in login session, not ASP.NET session) basis. The account under which the website is running probably doesn't have the drive letter mapped. Either use a UNC or log into the app pool account and mount the w: drive persistently.

Visual Web Developer (Express): Setting Document Root for Dev Environment

I'm developing a site in Visual Web Dev Express, and when I run/debug, I'd like to be able to set my application's document root so that I can use safer paths, like "/css/style.css' instead of "css/style.css". How would I accomplish this?
Click on the web site node in the solution explorer.
Press F4 to see the properties window.
Change the virtual path from /projectname to /
Bear in mind that this has an impact on how you expect the application/web site to be deployed. If it is ever used outside the root of a web server, the URL paths will be incorrect.
I have been looking for the virtual path field in the properties window but haven't been able to find it... it only shows me the following options
aperture directory URL
complete access route
policy file
port
use dynamic port
Do you know of some place else where I could find the Virtual Path field?
Select the "Web" tab and it's in the Servers section

Categories