简体   繁体   English

在ASP.NET Web窗体中的特定URL上启用CORS

[英]Enable CORS on Specific URL in ASP.NET Web Forms

I'm not a backend developer, so I apologize ahead of time if I do not provide enough information. 我不是后端开发人员,所以如果我没有提供足够的信息,我会提前道歉。 I am just trying to find some resources that will help my backend developer understand what I am trying to do. 我只是在寻找一些资源,这些资源将帮助我的后端开发人员理解我的工作。

I am trying to make an AJAX GET request to an rss feed inside of web application that was built using ASP.NET Web Forms. 我正在尝试向使用ASP.NET Web窗体构建的Web应用程序内部的rss提要AJAX GET请求。 However, the request gets blocked because of Cross Origin Security. 但是,由于跨源安全性,该请求被阻止。 I want to enable CORS for the route that is associated with our RSS feed ( /page/rss/{id} ). 我想为与RSS提要( / page / rss / {id} )相关的路由启用CORS。

I was able to enable CORS in our webconfig using: 我能够使用以下命令在我们的webconfig中启用CORS:

<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" values="*" />
        <add name="Access-Control-Allow-Methods" values="GET" />
        <add name="Access-Control-Allow-Headers" values="Content-Type" />
   </customHeaders>
</httpProtocol>

But, that enables it for the entire project which is not what I want. 但是,这使整个项目都可以使用,这不是我想要的。 Our routes are defined in an XML file like this: 我们的路线是在XML文件中定义的,如下所示:

<namespace.routing>
  <routings>
    <route name="publishedPage" url="page/{PageName}" page="~/Default.aspx" />
  </routings>
  <defaults>
    <default url="http://blog.example.com" domain="example.com" page="page/homepage" />
  </defaults>
</namespace.routing>

So, how would one go about enabling CORS on a specific path in ASP.NET Web Forms? 因此,如何在ASP.NET Web窗体的特定路径上启用CORS? If someone could point me in the direction of some resources that would help us that would be great. 如果有人可以向我指出一些对我们有帮助的资源,那将是很棒的。 If you need anymore information I'm happy to provide it. 如果您需要更多信息,我们很乐意提供。 Thanks! 谢谢!

I'm not sure how you are returning your rss endpoint, but if you have access to the HttpContext object, you can use it to supply the CORS headers directly. 我不确定如何返回rss端点,但是如果可以访问HttpContext对象,则可以使用它直接提供CORS标头。

HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", "*");
HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Methods", "Get");
HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Headers", "Content-Type");

Depending on how old your app is, you might need to use AddHeader instead of AppendHeader . 根据您的应用程序的年龄,您可能需要使用AddHeader而不是AppendHeader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM