0

I am developing an ASP.NET Web Api, and i would like to add a directory (lets say "exports") to be created on the server in which the application will store generated reports/logs etc.

I was advised that the "exports" directory should be a virtual directory so that the server admin could easily change the physical path to the stored files. Now i am not really sure of what is the proper way to deal with this.

The way i had implemented it was to create a directory in my project, put a dummy file in it and so let it be created on the server during publishing. But i don't know if that is the appropriate way to create application folders on a server, nor am i sure of the access restrictions that should be applied.

Additionally, this way creates a physical path relative to the application root, and the only way to make it virtual is to manually add a virtual directory with the same alias from the IIS server app pool manager.

So i would like to know of what is the proper way to create and manage a virtual directory on a server. Who is responsible for creating and managing the access permissions?

Should the application create it when it is deployed, or the admin be asked to create it manually using the server app pool admin panel?

EDIT :

Found a way to do it, just not sure if it's elegant or safe. As i've already mentioned, i am creating an empty "exports" directory during publishing (MyWebApi/exports). My app tries to access that directory using MapPath and Directory Classes :

if(Directory.Exists(System.Web.Hosting.HostingEnvironment.MapPath($"~/exports/")))
{
    Console.WriteLine("Export directory exists on server!");
}

So if nothing went wrong during web deploy the "exports" directory exists under the application root directory, and i can save my files in there.

Now if the Server admin would like to change the physical path of the exports directory and place it let's say at another disk on the server, he can go to the IIS manager, right click the application->Add virtual directory->Enter "exports" as alias and whatever they need as physical path. Please note that the alias must be exactly the same as the one created during publishing.

Now i have tested this, and provided that you grant the appropriate permissions to the new physical directory, it should work fine. But i have no idea if it's just a Monkey patch or a proper solution.

6
  • Maybe you should google it first, Microsoft have a proper article related to your questions, e.g. this Commented Nov 29, 2016 at 8:27
  • Thanks, I have already checked that link, but it doesn't answer the last part of my question. And i would like to avoid asking the server admin to create the directory manually, since i will have no access to the production server to do it myself. Commented Nov 29, 2016 at 8:32
  • You misunderstood the concept. If your web app saves files, they must go to a physical directory on the disk, and that has nothing to do with virtual directories at all. Virtual directories are used to serve incoming HTTP web requests. They are all called "directories" but not the same concepts. Commented Nov 29, 2016 at 12:47
  • @LexLi And what if the Server Admin needs to move the directory to another physical location e.g. another hard drive? Wouldn't he have to make a virtual directory that points to the desired physical path in order to preserve app functionality? I am just trying to provide an easy way of altering the "exports" directory physical path without breaking the web application. Thank you! Commented Nov 29, 2016 at 13:09
  • @ktsangop there is no such magic via IIS or Windows. But your code can dynamically read a setting to change its output folder. Commented Nov 29, 2016 at 13:46

1 Answer 1

1

Your best bet is to have someone create the virtual directory manually. Otherwise, the code for your site will have to have permissions to modify IIS, as well as write-permissions at the root level of your application path. Giving an application either of these permissions poses security risks. The folder only has to be set up once. Keep things simple!

You could use the virtual directory approach - that's fine, and it keeps you from having to put a static path into your web.config. But if you can avoid it, don't hardcode the virtual path (~/somefolder)into your code - leave it in the config file so it can be modified after deployment.

Sign up to request clarification or add additional context in comments.

3 Comments

You 're right, keep things simle! That way the server admin can edit the Web.config file and change the path after publishing/deployment correct?
@ktsangop Exactly.
thank you! Will wait a little bit for other comments, and then accept your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.