0

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>
AzeExMachina
  • 104
  • 14
  • 1
    That's because location tags are not entities mapped to .NET objects. The API is not designed as you wished. – Lex Li Nov 27 '20 at 17:07
  • So if I wanted to add that functionality via code my best bet would be to manually modify the xml? Or is there a better way? – AzeExMachina Nov 27 '20 at 17:12
  • The question to you is why you don't want to fully understand the design of `Microsoft.Web.Administration` API and follow its convention? To access a section (not all sections) in a location tag, use https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.administration.configuration.getsection?view=iis-dotnet#Microsoft_Web_Administration_Configuration_GetSection_System_String_System_String_ – Lex Li Nov 27 '20 at 17:19
  • I need to be able to add webdav publishing to my virtual directory, since I was already using the Microsoft.Web.Administration API to create sites and directories I thought I could also add the publishing with that – AzeExMachina Nov 27 '20 at 17:25

1 Answers1

1

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.

Bruce Zhang
  • 2,880
  • 1
  • 5
  • 11
  • Thanks, this is what I needed, based on your answer I was also able to enable the WebDav feature via code and also publishing for virtual directories of that site. – AzeExMachina Nov 30 '20 at 10:41