如何允许 ASP.NET WebForms 端点的 CORS? [英] How to allow CORS for ASP.NET WebForms endpoint?

查看:23
本文介绍了如何允许 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆