如何允许 ASP.NET WebForms 端点的 CORS?

How to allow CORS for ASP.NET WebForms endpoint?(如何允许 ASP.NET WebForms 端点的 CORS?)
本文介绍了如何允许 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)