web.config 配置
<system.webServer> <modules> <remove name="WebDAVModule" /> </modules> <handlers> <remove name="WebDAV" /> <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> <remove name="OPTIONSVerbHandler" /> <remove name="TRACEVerbHandler" /> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> <!--<add name="WebDAV" path="*" verb="PROPFIND,PROPPATCH,MKCOL,PUT,COPY,DELETE,MOVE,LOCK,UNLOCK" modules="WebDAVModule" resourceType="Unspecified" requireAccess="None" />--> </handlers> </system.webServer>
<appSettings> <add key="cors-origins" value="http://www.liuyanzhen.com" /> <add key="cors-headers" value="*" /> <add key="cors-methods" value="GET,POST,DELETE,PUT" /> </appSettings>
WebApiConfig.cs 配置
public static void Register(HttpConfiguration config) { // Web API 配置和服务 config.EnableCors(new EnableCorsAttribute( ConfigurationTool.AppSetting("cors-origins"), ConfigurationTool.AppSetting("cors-headers"), ConfigurationTool.AppSetting("cors-methods") )); // Web API 路由 config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApiAction", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); //config.Routes.MapHttpRoute( // name: "DefaultApi", // routeTemplate: "api/{controller}/{id}", // defaults: new { id = RouteParameter.Optional } //); }