【问题标题】:How can I set common headers of a virtual directory in IIS through C#?如何通过 C# 在 IIS 中设置虚拟目录的通用标头?
【发布时间】:2019-11-04 10:55:44
【问题描述】:

我必须将虚拟目录的“Expire Web Content”值设置为“立即”。这可以通过 IIS 实现,但我想使用 C# 脚本实现相同的结果。

我该怎么做?有什么线索吗?

【问题讨论】:

标签: c# http iis virtual-directory


【解决方案1】:

您可以通过将 system.webServer/staticContent/CacheControlMode 更改为 DisableCacheControl 来实现。

有两种方法可以实现。

1.如果你想在Site/MyVirtualDIR/web.config中设置

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample {

    private static void Main() {

        using(ServerManager serverManager = new ServerManager()) { 
            Configuration config = serverManager.GetWebConfiguration("Default Web Site" , "MyVirtualDir");

            ConfigurationSection staticContentSection = config.GetSection("system.webServer/staticContent");

            ConfigurationElement clientCacheElement = staticContentSection.GetChildElement("clientCache");
            clientCacheElement["cacheControlMode"] = @"DisableCache";

            serverManager.CommitChanges();
        }
    }
}

2.如果你想在root web.config/location中设置:

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample {

    private static void Main() {

        using(ServerManager serverManager = new ServerManager()) { 
            Configuration config = serverManager.GetWebConfiguration("Default Web Site");

            ConfigurationSection staticContentSection = config.GetSection("system.webServer/staticContent", "MyVirtualDir");

            ConfigurationElement clientCacheElement = staticContentSection.GetChildElement("clientCache");
            clientCacheElement["cacheControlMode"] = @"DisableCache";

            serverManager.CommitChanges();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多