Не могу заставить CORS работать в OWIN - PullRequest
0 голосов
/ 03 мая 2018

У меня есть следующий сервис. Нужно сделать перекрестный вызов. Посмотрел несколько статей, и кажется, что самое простое - это добавить appBuilder.UseCors (Microsoft.Owin.Cors.CorsOptions.AllowAll) в appBuilder. Однако после этого и изучения всего сетевого трафика я не вижу никаких заголовков «Access-Control *», вводимых в ответы.

Вот код:

// Start OWIN host 
        using (WebApp.Start(options, (appBuilder) =>
        {

            //// CORS section
            appBuilder.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

            // Http configuration
            HttpConfiguration config = new HttpConfiguration();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/",
                defaults: new { id = RouteParameter.Optional }
            );
            config.Formatters.Clear();
            config.Formatters.Add(new JsonMediaTypeFormatter());
            config.Formatters.JsonFormatter.SerializerSettings =
            new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };
            appBuilder.UseWebApi(config);

            string contentPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "assets");
            var fileServerOptions = new FileServerOptions
            {
                EnableDirectoryBrowsing = false,
                EnableDefaultFiles = true,
                DefaultFilesOptions = { DefaultFileNames = { "index.html" } },
                FileSystem = new PhysicalFileSystem(contentPath)                    
            };
            appBuilder.UseFileServer(fileServerOptions);
        }))
        {
            Console.WriteLine($"The service is running on {baseAddress}");
            Console.WriteLine("CTRL-C to exit...");
            WaitHandle.WaitOne();
        }
...