gpt4 book ai didi

asp.net - C# 从 web.config 中读取 customHeaders 值

转载 作者:行者123 更新时间:2023-12-02 02:59:04 26 4
gpt4 key购买 nike

我有需要在 WCF 中读取 customHeaders 值的要求。下面是我的应用程序的示例配置文件。我需要以编程方式找到“Access-Control-Allow-Origin”键的值。请帮助实现同样的目标。

<system.webServer>
<httpProtocol>
<customHeaders>
<add name="X-Content-Type-Options" value="nosniff"/>
<add name="Access-Control-Allow-Origin" value="http://localhost:4200"/>
<add name="Access-Control-Request-Method" value="POST,GET,PUT,DELETE,OPTIONS"/>
<add name="Access-Control-Allow-Headers" value="X-Requested-With,Content-Type"/>
<add name="Access-Control-Allow-Credentials" value="true"/>
</customHeaders>
</httpProtocol>

我在下面尝试过,但它不起作用。

Configuration config = serverManager.GetWebConfiguration("Web.Config");
ConfigurationSection httpProtocolSection = config.GetSection("system.webServer/httpProtocol");
ConfigurationElementCollection customHeadersCollection = httpProtocolSection.GetCollection("customHeaders");
foreach(var element in customHeadersCollection)
{
Response.Write(element.Attributes[0].Name);
}

最佳答案

我从 Microsoft 找到了这个示例来读取这些值。

https://learn.microsoft.com/en-us/iis/configuration/system.webserver/httpprotocol/customheaders/#sample-code

您需要引用 Microsoft.Web.Administration.dll 才能访问这些类。

      using (ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetWebConfiguration("Default Web Site");
ConfigurationSection httpProtocolSection = config.GetSection("system.webServer/httpProtocol");
ConfigurationElementCollection customHeadersCollection = httpProtocolSection.GetCollection("customHeaders");

// what you want is in customerHeadersCollection
}

关于asp.net - C# 从 web.config 中读取 customHeaders 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47593653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com