How to allow CORS for ASP.NET WebForms endpoint?(如何允许 ASP.NET WebForms 端点的 CORS?)
问题描述
我正在尝试将一些 [WebMethod] 带注释的端点函数添加到 Webforms 样式的 Web 应用程序(.aspx 和 .asmx).
I am trying to add some [WebMethod] annotated endpoint functions to a Webforms style web app (.aspx and .asmx).
我想用 [EnableCors] 注释这些端点,从而获得所有好的 ajax-preflight 功能.
I'd like to annotate those endpoints with [EnableCors] and thereby get all the good ajax-preflight functionality.
VS2013 接受注释,但端点仍然不能与 CORS 配合使用.(当使用同源而不是跨源时,它们工作正常).
VS2013 accepts the annotation, but still the endpoints don't play nice with CORS. (They work fine when used same-origin but not cross-origin).
我什至无法让它们在绒毛和脏污的情况下跨源运行
I can't even get them to function cross-origin with the down and dirty
HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*");
方法 -- 我的浏览器拒绝响应,并且不会出现跨域响应标头.
approach -- my browsers reject the responses, and the cross-origin response headers don't appear.
如何在这些 [WebMethod] 端点中获得 CORS 功能?
How can I get CORS functionality in these [WebMethod] endpoints?
推荐答案
我建议仔细检查您是否已执行此页面上的所有步骤:ASP.NET 上的 CORS
I recommend double-checking you have performed all steps on this page: CORS on ASP.NET
除了:
Response.AppendHeader("Access-Control-Allow-Origin", "*");
也试试:
Response.AppendHeader("Access-Control-Allow-Methods","*");
尝试直接在 web 配置中添加:
Try adding directly in web config:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Methods" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
否则,您需要确保您可以控制两个域.
Failing that, you need to ensure you have control over both domains.
这篇关于如何允许 ASP.NET WebForms 端点的 CORS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何允许 ASP.NET WebForms 端点的 CORS?
基础教程推荐
- C# 9 新特性——record的相关总结 2023-04-03
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 将数据集转换为列表 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 如果条件可以为空 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
