Referencing a virtual directory in C# - asp.net

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

Related

Bluimp Backload folder reference issue with virtual directories

I am working on a solution which has two projects in it. One is a virtual app which works in another. The first application is the panel and the second is the website. First application can be accessed with "localhost:10001/panel" and the second with "localhost:10001". You see, I created a virtual path for the first app to work under the second one in Visual Studio and they work great that way.
The problem I am having now is about the file uplaod system, "Blueimp's jQuery-File-Upload" plugin and as backend using "Backload". I must say that these work great on a standalone project. That's why I decided to continue the project using these.
But when it comes to a setup which I explained below, I cannot access the files I upload. I installed fileupload system in the panel project, which is accessed as "localhost:10001/panel" so when I leave the default web.config configuration for backload (default is "~/files"), all files are uploaded to the "localhost:10001/panel/files" path. And after the upload when I refresh the page, all uploaded file links are referencing "localhost:10001/files/" without the "panel" folder.
In BackLoad web.config notes how to change and use root upload folders are explained like that
filesRoot: // Root upload folder. If the value starts with '~/' (e.g. ~/files) the path is relative to the web root, otherwise set an absolute local path (e.g. d:/files) [Default: "~/Files"].
I understand that having "~/" at the beggining of a folder reference shows the project's root. But I can't figure out how to reference the upload folder, instead of the default "~/files", to upload and access all files from the second project's root. When I need to reference folder between these two projects I simple use "../", or "/" to access the second project's (site) root. But doing that in "filesRoot" attribute of BackLoad config settings, all file references are starting "///file....." and shows a local path in the computer.
I simply want to upload and access the files from the "localhost:10001/files" location when I upload files from the panel. Now, I cannot even use the "localhost:10001/panel/files" path because files are uploaded to "panel/files" folder, but are accessed from "files" folder with default settings.
BTW: I am using BackLoad's WebForms Example on this project, and this is a Web Forms project.
I ended up using Files folder as a temp folder. At the time of submit, I move the file which is uploaded in Files folder, to the folder of my need. And the problem is solved.
Thanks anyway...

HostingEnvironment.VirtualPathProvider.FileExists returns false on existing file

I'm trying to read a file in a virtual path, using the following code
HostingEnvironment.VirtualPathProvider.FileExists(_SiteMapFileName)
_SiteMapFileName has something like "~/Content/en-US/MainMenu.sitemap" and it is always returning false.
But if I access the URL of that virtual path (http://www.local.mysite.com/Content/en-US/MainMenu.sitemap), the file displays correctly in the browser
Content is a virtual directory under my website, pointing to a shared folder on a different location. If I access that location on Windows Explorer I can see the files correctly
I've set the site and the virtual directory to connect as my network account.
I used the process monitor tool to see if there's any problem and it says that w3wp.exe is trying to create the file \path\to\shared\location\Content\en-US\MainMenu.sitemap but it is getting ACCESS DENIED which is fine since it is a read-only location
Does anybody now how to solve this issue?
I found out the reason. The user set to run the app pool tied to the application needs to have permissions on the file is trying to read, not only the website and the virtual directory. :/
when i am working with vDir i use Server.MapPath
Does that not work for you?
if(System.IO.File.Exists(Server.MapPath("Relative Path to vDir")))
{
//do something with the file
}

Upload files to different virtual directory

I have two web application hosted on different server.For eg. one is main application and the other one is branch application.
In branch application, user will upload their files but we want to keep all uploaded files in main application virtual directory.
So that we put the main application virtual directory path while we upload the files.But We got error message like "Invalid Directory" and can't upload.
Is there any way to upload files from one application to another directly? We are using normal html upload control in asp.net and visual studio 2008.
Code Sample :
main application virtual directory "http://10.10.10.1/mainapp/uploadedfiles/"
branch application virtual directory "http://10.10.10.2/branchapp/"
HttpPostedFile postedFile;
string saveFile = Path.Combine("http://10.10.10.1/mainapp/uploadedfiles/", "File1.pdf");
postedFile.SaveAs(saveFile);
Please guide me the right way and I really appreciated it.
Thanks.
Best Regards,
Chong
In the FileUpload control, the FileUpload.SaveAs(path) method takes a physical path, not a virtual path. So if you have a virtual path, you will need to use the Server.MapPath method to convert it to a physical path. Using your example, you might want to change the last line of your code to:
postedFile.SaveAs(Server.MapPath(saveFile))
Also, you will need to make sure that the account that ASP.Net is running under has ACL write permissions on the physical directory.
I think u can share the directory and ensure both the Apps can access it, because the two apps are in different servers, the directory path maybe just like : \serverA\Files\, and the url of this directory is : http://localhost/files. u can save files to other servers by a relative path if u have the permission.

Need help setting up simple Virtual Directory in IIS7

Noobie question...
Using IIS7, I am trying to create a virtual directory for the folder that contains my video files, but can't get my head around how it is done.
For example...
The existing address is http://www.mydomain.com/members
which points to C:\wwwroot\mydomain\members
I need http://www.mydomain.com/flash-members to point to the same path.
The existing IIS path to the members folder is Server\Sites\www_mydomain_com\members (has application icon)
Any help is appreciated.
For your example, assuming www_mydomain_com is the Site (little world icon), you can do this by:
Open IIS Manager
Right-click the web site, select Add Virtual Directory
In the Alias field, enter flash-members
In the Physical Path field, enter your path (C:\wwwroot\mydomain\members)

Server.MapPath not returning expected path

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.

Resources