简体   繁体   English

使用ASP.net在IIS上创建虚拟目录的正确方法

[英]Proper way of creating virtual directories on IIS using ASP.net

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. 我正在开发ASP.NET Web Api,并且我想在要在其中存储应用程序生成的报告/日志的服务器上添加要创建的目录(让我们说“导出”)。

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. 此外,此方法创建相对于应用程序根目录的物理路径,并且使其变为虚拟状态的唯一方法是从IIS服务器应用程序池管理器手动添加具有相同别名的虚拟目录。

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). 正如我已经提到的,我在发布期间创建了一个空的“导出”目录(MyWebApi / exports)。 My app tries to access that directory using MapPath and Directory Classes : 我的应用尝试使用MapPath和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. 因此,如果在Web部署过程中没有出错,则“ exports”目录存在于应用程序根目录下,我可以将文件保存在该目录中。

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. 现在,如果服务器管理员想要更改导出目录的物理路径并将其放置在服务器上的另一个磁盘上,则可以转到IIS管理器,右键单击应用程序->添加虚拟目录->输入“导出” ”作为别名,以及将其用作物理路径所需的任何内容。 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. 但是我不知道这只是猴子补丁还是合适的解决方案。

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. 否则,您网站的代码将必须具有修改IIS的权限以及在应用程序路径的根级别的写入权限。 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. 您可以使用虚拟目录方法-很好,它使您不必在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. 但是,如果可以避免,请不要将虚拟路径( ~/somefolder )硬编码到代码中-请将其保留在配置文件中,以便在部署后可以对其进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM