1. Search Webapi in NuGet to find the following extension and add it into the project.
2. Add a line of code in Global.asax
protected void Application_Start () { // add cors support GlobalConfiguration.Configuration.EnableCors (); // other things Arearegistration.registerallareas (); Webapiconfig.register (globalconfiguration.configuration); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (bundletable.bundles); }
3. Add the configuration code at the top of the controller
" * " " * " " * " )]publicclass Default1controller:apicontroller
* The symbol allows all.
Origins represents which sites are allowed to access your API, if you only allow Baidu to access your API, you can set this up, if there are multiple uses, separate
"http://www.baidu.com,http://www.qq.com"* "*")]
Headers,methods the custom header and HTTP methods allowed by the requested request
Allow Webapi to support cors, cross-domain access