我有asp.net Web表单应用程序,我想只为特定域启用CORS。我试图使用HttpModule实现这一目标。
例如我的网站是https://mysiteex.com
这可能是你正在寻找的。将using System.Web;
和下面的代码放入Application_BeginRequest事件的Global.asax文件中。
protected void Application_BeginRequest(object sender, EventArgs e)
{
// Enable CORS for cross-site scripting.
var context = HttpContext.Current;
var response = context.Response;
// enable CORS. "*" = all domains, substitute your own as needed.
response.AddHeader("Access-Control-Allow-Origin", "*");
if (context.Request.HttpMethod == "OPTIONS")
{
response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
response.End();
}
}