首页 > 代码库 > asp.net webapi支持跨域

asp.net webapi支持跨域

1、Install-Package Microsoft.AspNet.WebApi.Cors

2、
using System.Web.Http;
namespace WebService
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.EnableCors();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

3、
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace WebService.Controllers
{
    [EnableCors(origins: http://www.***.com, headers: "*", methods: "*")]
    public class TestController : ApiController
    {
            }
}

asp.net webapi支持跨域