C# ASP.NET3.5中的CORS

C# ASP.NET3.5中的CORS,c#,jquery,asp.net,cors,C#,Jquery,Asp.net,Cors,我已经在ASP.NET3.5中创建了一个基本的web服务。现在,当我尝试从不同的域访问它时,它会给我错误。我尝试了很多方法来启用CORS,但都失败了。请帮我整理一下 dealsfeed.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Data.SqlClient; using Man.Ut

我已经在ASP.NET3.5中创建了一个基本的web服务。现在,当我尝试从不同的域访问它时,它会给我错误。我尝试了很多方法来启用CORS,但都失败了。请帮我整理一下

dealsfeed.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using System.Data.SqlClient;
using Man.Utility;
using Man.Helper;

using System.ServiceModel.Web;

/// <summary>
/// Summary description for dealsfeed
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class dealsfeed : System.Web.Services.WebService {

    public dealsfeed () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        try
        {
            return "Hello World";
        }
        catch (NullReferenceException ex)
        {
            return "Processor Usage" + ex.Message;
        }
    }

}

请把这篇文章通读一遍。您需要配置IIS以启用它。如果没有,可以使用以下代码启用:

Response.AppendHeader("Access-Control-Allow-Origin", "*");
将此代码添加到您的配置中:

public static void Register(HttpConfiguration config)
{
    // New code
    config.EnableCors();
}
要启用跨源请求,请将[EnableCors]属性添加到Web API控制器或控制器方法:

[EnableCors(origins: "http://example.com", headers: "*", methods: "*")]
public class TestController : ApiController
{
    // Controller methods not shown...
}
上述方法也可用于跨API启用CORS,而无需注释每个控制器:

public static void Register(HttpConfiguration config)
{
    var corsAttr = new EnableCorsAttribute("http://example.com", "*", "*");
    config.EnableCors(corsAttr);
}
[EnableCors(origins: "http://example.com", headers: "*", methods: "*")]
public class TestController : ApiController
{
    // Controller methods not shown...
}
public static void Register(HttpConfiguration config)
{
    var corsAttr = new EnableCorsAttribute("http://example.com", "*", "*");
    config.EnableCors(corsAttr);
}