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

Configuring WebDav through ServerManager

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

I want to configure my IIS virtual folder to enable WebDav publishing. I can easily do it manually but I wanted to do the same thing via code, in C#. I have no problem in creating sites or virtual directories and even adding properties such as mime types to them, but I can't seem to access the WebDav settings that should be in my applicationHost.config file. I've made multiple attempts like this. Obviously this snippet does nothing, it's only there to show you what I was toying with.

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}");
    }
}

I've tried using all those methods but I can't seem to access these sections which are located at the end of my applicationHost.config file. The second one is my virtual folder. WebDav functionality is active, I've tried connecting to it by using some applications and it works fine. What can I do? Thanks in advance.

<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

My test proves that this operation can be done using code, including the configuration of authoringRules in the virtual directory.

 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);
            }
        }

So I think your code does not perform any operations, it may be something wrong. I suggest you look for problems in debug mode.

For more configuration through code, please refer to this document.