Accessing Files on Network from IIS - asp.net

I have a shared path \\mynetworkshare\myfolder which has images stored.
I have a domain user mydomainuser which was granted with read access to files in that path
I hosted website on IIS. I created a Virtual Directory within my IIS Site with an Alias myphotos pointing to the Physical path \\mynetworkshare\myfolder. I have also clicked on Connect as... button in the Add Virtual Directory dialog box and provided my mydomainuser credentials.
I clicked on the newly created virtual director myphotos and click on Content View in the right pane. I'm able to view all my photos within IIS. From this, I assume the setup of virtual directory to my shared drive is correct.
Now, the question is how do I access this Virtual Directory or Files in it from my code?
I have tried below
var filePath = Server.MapPath("~/myphotos/" + "myimage.jpg");
When I write the filePath to a log file, I see it is trying to map to a physical folder setup within my website folders.
Instead of pointing to
\\mynetworkshare\myfolder\myimage.jpg
it is pointing to
d:\wwwroot\inetpub\mywebsitefolder\myphotos\myimage.jpg
I know Server.MapPath resolves to a physical path of hosted site but I wonder if it behaves the same with my virtual directory.
Or Do I need to let ASP.Net know somehow that myphotos is a virtual directory created on IIS? Or Am I on the wrong path to get files? Do I need to write code something different?

You can get the physical path from IIS (7+) using System.Web.Administration (available in NuGet)
var physicalPath = new Microsoft.Web.Administration.ServerManager()
.Sites["Default Web Site"]
.Applications["/MyApplication"]
.VirtualDirectories["/MyVirtualPath"]
.PhysicalPath;
If the virtual directory is in the root, the Application is "/"
What's specified in the Connect as... may only apply to direct web requests. To access the files from your application, you will need to configure the Application Pool to use mydomainuser as its Identity (found under Advanced Settings...), or you will need to grant share permissions to the computer running IIS if the Application pool uses a built-in account.

The step that was missing is converting Virtual Folder as Application.
Right-click the Virtual Folder myphotos, click on Convert to Application
The link here should explain the steps
Without adding Server.MapPath, it works fine. For example
image.src = "~/myphotos/myimage.jpg"

Related

How to deploy web application as a child/sub application in Azure? [duplicate]

I have an application running on Azure, what I need is to have the application copied so different users can access for example site.com/s1 and site.com/s2 and use them as separate. The application is the same, the database will change.
I tried to create a virtual directory for my app but I get the same error every time: "The physical path for virtual path '/s1' is invalid."
I tried creating a physical directory from FTP and I can see the new directory, but Azure don't recognize it as a physical one.
I would like to have wwwroot/s1 wwwroot/s2 and access those apps the way I explained before.
Please have a try to config the Virtual directory site/test1 on the Azure portal as following screenshot
When we push the WebApp to the Azure, we need to include the virtual directory path in the Site Name and Destination URL sections on the Connection tab
After that we can visit the WebApp with sitename/site1
We also can check the uploaded file from Azure Kudu tool (https://sitename.scm.azurewebsites.net/DebugConsole/?shell=powershell)

Check if files are present on same server asp.net

I am working on Asp.net Website. I want to check if file are there on same server deployed application. How can i check that by physical path or virtual path ?
Eg: My website is deployed on Server A in directly Web1 which is under D drive.
I have one more website deployed on same server but different directory WEB2.There is folder in WEB2 directory say attachment ,which have all the media files eg pdf,doc etc.
So how can i check from Web1 that files are there in WEB2/Attachment folder ??
If they're on the same server, you can use File.Exists(file..)
If they're on different servers but still within the same network, map a drive or use UNC format when using File.Exists
If they're on different servers with no way of being able to see each other's drives, create a webservice within each site; add a method that takes a filename as a parameter, the webservice itself can do a File.Exists
Make sure that your application pool has access to the attachment folder. I think the app pool by default doesn't have access to a folder outside the web1 directory. Then you can execute the following code to get the list of files in the attachment folder.
string[] fullPathToFiles = Directory.GetFiles("D:\web2\attachment\")
or you can execute
File.Exists("D:\web2\attachment\<file.pdf>")
as sh1rts has suggested.

Creating a virtual path in asp.net/Azure

We are moving a web app to the MS Azure environment and had to change where photos are stored. Azure uses a blob environment that we access via a mapped cloud drive. We dynamically set the drive letter at app start. All of this is working fine and the pictures save correctly.
We need to set a URL path to these pictures so that users can view them. In the old app, that worked fine because they were stored within the app environment but now they are outside. How can I set a mapping for something like http://myapp.com/photos/abc.jpg will point to h:\abc.jpg?
It sounds like all you need to do is create a virtual directory in IIS in the root of your application called photos that is mapped to h:\.
To do this, open IIS Manager, right-click on your site, and select Add Virtual Directory.... Set the alias to photos and the physical path to h:\.

why it is still necessary to create virtual directory in IIS when website content is placed in IIS root directory

As virutal directory points to physical path of the application, so if the IIS root directory is C:\inetpub\wwwroot and the application is stored at D:\websites, than we need to create a virtual directory but if the application content is placed at C:\inetpub\wwwroot, then why still need to create virtual directory.
Actually you don't need to create a virtual directory. What you need to do is define it as an application folder, adding a virtual directory does this by default so that's why it works. All you really need to do is right click the folder under your website, click properties and under the Application Settings section click create. Your folder will then have a gear icon off to the side denoting it as an application folder.
As to why application folders are necessary, I believe it's a way of forcing you to choose which Application Pool you want the application running in rather than having every sub directory application running off the same pool by default.
IIS isn't used just for ASP.Net, it can serve up PHP for example...so the type of application and pool varies, it's not automatically created and tied to then ASP.Net engine.
If you can clarify a bit more what you're trying to do maybe we can help further. For example, if you want to point the root application to another folder and it be an application or create another website, IIS allows you to do any of that...you just need to update the question with which version of IIS, as the instructions very between them.
If the IIS site will host a single ASP.NET application you can place it at the root (C:\inetpub\wwwroot) and you don't need to create a virtual directory because when you create the site it is already a virtual directory.

IIS7 Creating Virtual Directory to files on another server

I am migrating some ASP.Net applications from IIS6 to IIS7 and all has gone well until now. I am trying to create several virtual directories on 1 server that will point to files on another server. In IIS6, all I had to do was make anonymous authentication use a user that had access to the files. In IIS7, I can't get it to work. I setup the virtual directory with the username and password but I get a 500.19 error "Cannot read configuration file due to insufficient permissions". I can't imagine that I really have to put a web.config in a directory that just contains images and no application files. If I click on the directory in IIS Manager, I get that error about the web.config file on anything that I click on such as the Authentication section. Also, writing to files from the virtual directory seems to work just fine so I don't think there is anyway that I have an access problem. I have given the directory access to the IIS_IUSR group.
When using a virtual directory that's pointed to a UNC share, go to advanced settings for the virtual directory, then choose a specific user account that has the appropriate rights to access the folder.
It works for my virtual directory in my asp.net app that is pointed to a UNC share on a remote server.

Resources