获取web.config中Access-Control-Allow-Origin的值的一种方法

时间:2020-07-30 15:26:23   收藏:0   阅读:155

  对webapi1,找了好久没看见比较好的获取Access-Control-Allow-Origin值的方法,多方搜索后,提出下面一种,至少可以完成目标:

Configuration webConfig = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection cs = webConfig.GetSection("system.webServer") as ConfigurationSection;

bool haveOriginInInWebConfig = false;
if (cs != null)
{
XDocument xml = XDocument.Load(new StringReader(cs.SectionInformation.GetRawXml()));
foreach (XElement e in xml.Descendants("add"))
{
if(e.FirstAttribute.Value.Equals("Access-Control-Allow-Origin"))
{
haveOriginInInWebConfig = true;
break;
}
}
}

原文:https://www.cnblogs.com/dusf/p/13403678.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!