2016-03-29 8 views
0

this, sunucudaki her bir kaynak için ayrı bir CORS ön kontrol isteğine gereksinim duyduğunuzu ve özel üstbilgileri (içerik türü JSON) gerektiren bir RESTFul API'siyle çalıştığımdan emin olun. Ancak her istek için (neredeyse) iki tur gezmekten kaçınmak isterim.Birden fazla CORS kaynağını manuel olarak önceden kontrol etmek mümkün mü?

İdeal çözümüm, bir istekte birden fazla kaynağı ön kontrol etmek olacaktır. Web uygulamasına /user, /media ve /preferences POST uygulamasına izin vermek istesem, bu istekleri ilk gönderilmeden önce bile bir istekte yapabiliriz, aksi takdirde korkunç gecikme eklenir. Bu CORS ile mümkün mü?

cevap

0
public static class WebApiConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 
      // Web API configuration and services 
      // Configure Web API to use only bearer token authentication. 
      //config.SuppressDefaultHostAuthentication(); 
      //config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); 

      // Web API routes 
      config.MapHttpAttributeRoutes(); 
      //var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter; 
      // xml.UseXmlSerializer = true; 

      //var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter; 
      config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); 
      //json.SerializerSettings.PreserveReferencesHandling = 
      // Newtonsoft.Json.PreserveReferencesHandling.All; 
      //json.SerializerSettings.DateFormatHandling 
      //= Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat; 
      config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 

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

//And on WEBAPI your controller try this. 

[EnableCorsAttribute("http://localhost:8080/", "*", "*")] 
    public class SampleController : ApiController 
    { 
     // GET: api/Club 

     public IEnumerable<Sample> Get() 
     { 
      var SampleRepository = new SampleRepository(); 
      return SampleRepository.Retrieve(); 
     } 

     // GET: api/Sample/id 
     public string Get(int id) 
     { 
      return "value"; 
     } 

     // POST: api/sample 
     public void Post([FromBody]string value) 
     { 
     } 

     // PUT: api/sample/id 
     public void Put(int id, [FromBody]string value) 
     { 
     } 

     // DELETE: api/sample/id 
     public void Delete(int id) 
     { 
     } 
    } 

//In AngularJS you will get what you want this way using CORS through WEB API 

(function() { 
    "use strict"; 
    angular 
     .module('sampleManagement') 
     .controller('SampleListController', 
        ['sampleResource', 
         SampleListController]); 

    function SampleListController(sampleResource) { 
     var vm = this; 

     sampleResource.query(function (data) { 
      vm.Sample = data; 

     }); 
    } 

}()); 



(function() { 
    "use strict"; 

    var app = angular.module('sampleManagement', 
     ['common.services']); 

}()); 


(function() { 
    "use strict"; 
    angular.module('common.services', 
     ['ngResource']) 
    .constant('appSettings', 
    { 
     serverPath: "http://localhost:8080/" 
    }); 
}()); 



(function() { 
    "use strict"; 
    angular 
     .module('common.services') 

     .factory('sampleResource', 
     ['$resource', 
      "appSettings", 
     sampleResource]) 

    function sampleResource($resource, appSettings) { 

     return $resource(appSettings.serverPath + "api/Club/:id"); 

    } 
    app.config(['$resourceProvider', function($resourceProvider) { 

     $resourceProvider.defaults.stripTrailingSlashes = false; 
    }]); 
}()); 
İlgili konular