sofu6

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

  对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;
}
}
}

posted on 2020-07-30 14:40  sofu6  阅读(574)  评论(0编辑  收藏  举报