Creating a virtual path in asp.net/Azure - asp.net

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:\.

Related

HTML 5 audio with src to virtual directory with credentials

I have an asp.net application which does the following: loads data about audio files (file name, path and so on) from DB and shows them in table on the page. Application uses windows authentication (this cannot be changed).
I've added html 5 audio controls to my table cells so that users could play audio files without downloading them - actually, I don't want them to be able to download files at all.
Audio files come from two network-shared directories - one is accessible by all domain users, the second one is accessible only to specific user. Audio control requires the src to be a relative url - ok, fine, so I went to IIS manager and created two virtual directories under my application. For the second, highly protected directory, I'd set credentials which should have helped my application to access files.
Result seems quite strange to me: users see audio files from the first (accessible) directory preloaded and can play them; users cannot preload and cannot play files from the second directory with preset credentials.
Problem is definitely not in the audio src itself - it is well-formed. Seems like IIS is trying to access data in the second virtual directory under credentials of current domain user and not the one, whose login and password were set on folder setup.
Is there any way to overcome this problem? Unfortunately, granting access to this directory to all users is not a chance. Changing app pool identity to that same specific user also gave no results. App users and user account for the second folder are all in the same domain.
IIS access file via application pool identity not login user. So when you are running an application with network share directory, please change the app pool identity to a domain user that have permission to access both. Then you can restrict login user permission via authorization rule.

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)

Azure multiple websites, subdomains and virtual directories

I have a front end application and an admin application in a dedicated windows server on IIS. The admin has an images virtual directory pointing to the front end images directory so that any images uploaded through the admin uploads to the front end directory. Also the admin is a sub domain of the front end website. I am trying to replicate this structure in Azure.
So far what I've tried:
Created two different web apps and mapped the external domains say, www.contoso.com and admin.contoso.com with no problem. But if I need to create a virtual directory in the admin web app, it doesn't allow me to reference anything outside the site\wwwroot folder of the admin web app.
Created two directories in the same web app but cant find a way to map www.contoso.com and admin.contoso.com to two different directories in the same web app.
Uploaded the front end to the site\wwwroot and created a sub directory called admin and mapped www.contoso.com and admin.contoso.com to the same web app and used url rewriting to point the admin sub directory to admin.contoso.com. But still can't find a proper way to add a virtual directory in the admin to point to the front end.
I am using the new portal and the applications use ASP.NET MVC, .NET 4.5 by the way.
Can anyone please point me in the right direction of how to manage and configure multiple websites with subdomains and virtual directories?
Try this - in your 1st configuration. Go to Azure Web apps settings -> Application Settings -> Virtual Applications and directories and setup the physical path of the new folder(Other than the site/wwwroot). Also check the Application checkbox and restart the website once.
I've used the third approach from my question where I created a sub directory for the admin. I would still like to know how this can be done using other approaches.
So I had created a virtual application for the admin already which is mapped to the subdomain. I was trying to add a virtual directory for admin/content/images/frontend to point to the front end site\wwwroot\content\images. I was doing this in one single line of virtual directory setting which was complaining that the path was invalid. So I had to create a virtual directory for each admin/content mapped to site\wwwroot\admin\content, admin/content/images mapped to site\wwwroot\admin\content\images and then finally admin/content/images/frontend mapped to site\wwwroot\content\images which worked.

Accessing Files on Network from IIS

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"

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.

Resources