public static class WebApiConfig { public static void Register(HttpConfiguration config) { var setting = System.Configuration.ConfigurationManager.AppSettings; var cors = new EnableCorsAttribute("http://192.168.3.15:3000,http://192.168.3.15:3002", "*", "GET,POST,PUT,DELETE,OPTIONS") { SupportsCredentials = true }; config.EnableCors(cors); // Web API 路由 config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/", defaults: new { id = RouteParameter.Optional }, constraints: new { id = @"d+" } ); } }
或者 但在config不可以配置多个ip地址
<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="http://192.168.6.43:3000" /> <add name="Access-Control-Allow-Headers" value="x-requested-with,content-type" /> <add name="Access-Control-Allow-Method" value="GET,POST,OPTIONS" /> <add name="Access-Control-Allow-Credentials" value="true" /> </customHeaders> </httpProtocol> </system.webServer>