首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过ServerManager配置WebDav

通过ServerManager配置WebDav
EN

Stack Overflow用户
提问于 2020-11-27 23:31:57
回答 1查看 43关注 0票数 0

我要配置我的IIS虚拟文件夹以启用WebDav发布。我可以很容易地手动完成,但我想通过C#的代码来做同样的事情。我在创建站点或虚拟目录,甚至向它们添加属性(如mime类型)方面都没有问题,但我似乎无法访问本应在applicationHost.config文件中的WebDav设置。我已经做了很多次这样的尝试。显然,这段代码什么也不做,它只是为了向您展示我在玩什么。

代码语言:javascript
复制
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功能是激活的,我尝试过使用一些应用程序来连接它,它工作得很好。我能做什么?提前谢谢。

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-30 11:55:49

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

代码语言:javascript
复制
 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);
            }
        }

所以我认为你的代码没有执行任何操作,可能出了什么问题。我建议你在调试模式下查找问题。

更多代码配置请参考此document

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65039896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档