如何通过C#在IIS中设置虚拟目录的通用标头?

如何通过C#在IIS中设置虚拟目录的通用标头?

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

我该怎么做?有任何线索吗?

proxool 回答:如何通过C#在IIS中设置虚拟目录的通用标头?

您可以通过将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。如果要在根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();
        }
    }
}
本文链接:https://www.f2er.com/3167360.html

大家都在问