Warm tip: This article is reproduced from serverfault.com, please click

c#-通过ServerManager配置WebDav

(c# - Configuring WebDav through ServerManager)

发布于 2020-11-27 15:31:56

我想配置IIS虚拟文件夹以启用WebDav发布。我可以轻松地手动完成此操作,但是我想通过代码在C#中执行相同的操作。在创建站点或虚拟目录,甚至向它们添加mime类型之类的属性时,我都没有问题,但是我似乎无法访问应该在我的applicationHost.config文件中的WebDav设置。我已经做了多次这样的尝试。显然,此代码段不执行任何操作,只是在向你显示我在玩弄什么。

using (var serverManager = new ServerManager())
{
    try
    {
        var app = serverManager.Sites.FirstOrDefault(o => o.Name.Equals("custom"))?.Applications.First(o => o.Path.Equals("/"));
        var config = serverManager.GetApplicationHostConfiguration();
        var web = serverManager.GetWebConfiguration("custom");
        var setting = config.RootSectionGroup;
        var admin = serverManager.GetAdministrationConfiguration();

        serverManager.CommitChanges();
    }
    catch (Exception e)
    {
        Console.WriteLine($"Error when creating virtual directory: {e.Message}");
    }
}

我尝试使用所有这些方法,但似乎无法访问位于applicationHost.config文件末尾的这些部分。第二个是我的虚拟文件夹。WebDav功能处于活动状态,我已经尝试通过使用某些应用程序连接到它,并且效果很好。我能做些什么?提前致谢。

<location path="custom">
    <system.webServer>
        <security>
            <requestFiltering>
                <fileExtensions applyToWebDAV="false" />
                <verbs applyToWebDAV="false" />
                <hiddenSegments applyToWebDAV="false" />
            </requestFiltering>
        </security>
        <webdav>
            <authoring enabled="true">
                <properties allowAnonymousPropfind="true" />
            </authoring>
        </webdav>
    </system.webServer>
</location>
<location path="custom/local">
    <system.webServer>
        <webdav>
            <authoringRules>
                <add users="*" path="*" access="Read" />
            </authoringRules>
        </webdav>
    </system.webServer>
</location>
Questioner
AzeExMachina
Viewed
0
Bruce Zhang 2020-11-30 11:55:48

我的测试证明,可以使用代码来完成此操作,包括在虚拟目录中配置authoringRules。

 using (ServerManager serverManager = new ServerManager())
        {
            try
            {

                Configuration config = serverManager.GetApplicationHostConfiguration();

                ConfigurationSection authoringRulesSection = config.GetSection("system.webServer/webdav/authoringRules", "Default Web Site/a");

                ConfigurationElementCollection authoringRulesCollection = authoringRulesSection.GetCollection();

                ConfigurationElement addElement = authoringRulesCollection.CreateElement("add");
                addElement["roles"] = @"administrators";
                addElement["path"] = @"*";
                addElement["access"] = @"Read, Write, Source";
                authoringRulesCollection.Add(addElement);

                serverManager.CommitChanges();
                Console.WriteLine("success");
            }
            catch (Exception e)
            {
                Console.WriteLine("failed"+e.Message);
            }
        }

因此,我认为你的代码不执行任何操作,这可能是错误的。我建议你在调试模式下查找问题。

有关通过代码进行更多配置的信息,请参考此文档