웹 API 프로젝트에서 CORS를 이해하고 사용하는 것이 매우 어렵습니다. 차단 지점에 도달했습니다. ASP.NET ID를 사용하여 ASP.NET MVC Web API 2 프로젝트를 시작했습니다. 내가하는 것이 무엇이든간에 효과가없는 것 같습니다.웹 API 2.0에서 Cors가 작동하지 않습니다.
내 global.asx
파일을 삭제 한 내 시작은 다음과 같습니다
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration configuration = new HttpConfiguration();
// I'm not sure it this is the proper way to use webapiconfig
WebApiConfig.Register(configuration);
app.UseWebApi(configuration);
app.UseCors(CorsOptions.AllowAll);
ConfigureAuth(app);
}
}
과 WebApiConfig.Register 코드는 다음과 같습니다
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));
config.AddODataQueryFilter();
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
RegisterModels(); //Automapper here
}
나는 엠씨 ASPNET의 고르과 마이크로 소프트 owin 호스트이 시스템 웹이 설치되었습니다. 은 "[조립체 OwinStartup (대해서 typeof (MyProject.Startup))]의"I web.config
에 가지고 장소에 있고 :
<appSettings>
<add key="owin:AutomaticAppStartup" value="true" />
</appSettings>
I에서만 호출 app.UseCors (CorsOptions.AllowAll)는 CORS 있도록, config.enableCors
또는 아무것도하지만 때마다 나는 API에서 토큰 또는 아무것도를 받고 시도와 같은 다른 방법은, 나는 오류 얻을 : 나는 Configuration
방법에 중단 점을 넣어 시도
Reason: CORS header ‘Access-Control-Allow-Origin’ missing.
을 있지만 호출되지 않습니다. .. 이제까지. IIS Express로 디버깅 중입니다.
그게 나를 위해 일한 유일한 해결책입니다. 감사! – alvipeo
감사합니다. 프로덕션 배포를 위해 무엇을 제안 했습니까? – Lobato
안녕하세요 Lobato, 어디에서 전화가 올지 알면 해당 사이트 발신지와 함께 * 변경하십시오. *는 '아무 데나'를 의미하기 때문에 또한 일부 api는 원점 외부에서 'GET'만 허용하고 CRUD 작업에는 도구를 사용해야합니다. 변경된 'Access-Control-Max-Age'는 브라우저 특정 값으로 대체되기 때문에 무시할 수 있습니다. (예 : Chrome에서 10 분). –