asp.net 在visual studio development server上为soap web服务启用CORS设置

x4shl7ld  于 2023-10-21  发布在  .NET
关注(0)|答案(2)|浏览(93)

我试图通过启用“控制-允许-起源”在Web服务方法如下所示的头添加CORS设置。但是,我仍然得到错误:请求的资源上不存在“HTTP-Control-Allow-Origin”标头。

[ScriptMethod(UseHttpGet = true)]
        [WebMethod]
        public ClientData[] GetClientData(int Number)
        {

            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost:52630");

            ClientData[] Clients = null;

            if (Number > 0 && Number <= 10)
            {
                Clients = new ClientData[Number];
                for (int i = 0; i < Number; i++)
                {
                    Clients[i].Name = "Client " + i.ToString();
                    Clients[i].ID = i;
                }
            }

            return Clients;
        }
kyks70gy

kyks70gy1#

将以下内容添加到您的Web.config文件中:

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin"
           value="*" />
      <add name="Access-Control-Allow-Credentials"
           value="true" />
      <add name="Access-Control-Allow-Headers"
           value="SOAPAction, Content-Type,Authorization" />
      <add name="Access-Control-Allow-Methods"
           value="GET, POST, OPTIONS" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

详情请查看https://www.codeproject.com/Questions/827596/Cross-domain-SOAP-call-with-XmlHttpRequest-to-a-we

dwthyt8l

dwthyt8l2#

把这个放到你的web.config里
如果需要,您可以进行调整。这个例子我只为.aspx打开

<configuration>
  <system.web>
      <httpHandlers>
        <add verb="GET,HEAD,POST,OPTIONS" path="*.aspx" type="System.Web.UI.PageHandlerFactory" />
      </httpHandlers>
  </system.web>
</configuration>

你可能也需要这样的东西。

if (Request.HttpMethod == "OPTIONS")
    {
        Response.AppendHeader("Access-Control-Allow-Origin", "*");
        Response.AppendHeader("Access-Control-Allow-Headers", "Content-Type");
        return;
    }

相关问题