简体   繁体   English

Angular.js-CORS-ASP.NET-Rest API-在Access-Control-Allow-Origin标头中找不到原始

[英]Angular.js - CORS - ASP.NET - Rest API - Origin not found in Access-Control-Allow-Origin header

Currently working on a web page that does a call to a rest api which hits my SQL server. 当前在一个网页上运行,该网页调用了一个REST API,该API击中了我的SQL Server。 Works on local host in VS2013 however when published and added to the server I began to run into issues with CORS. 在VS2013中的本地主机上工作,但是当发布并添加到服务器时,我开始遇到CORS问题。 I have added the following code to my web.config file for my web service and still no luck: 我已将以下代码添加到我的Web服务的web.config文件中,仍然没有运气:

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Methods" value="GET" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
  </customHeaders>
</httpProtocol>

My webapiconfig.cs looks like this with Cors enabled: 启用Cors后,我的webapiconfig.cs如下所示:

 public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.EnableCors();
        var json = config.Formatters.JsonFormatter;
        json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
        json.SerializerSettings.ContractResolver = new
            CamelCasePropertyNamesContractResolver();
        config.Formatters.Remove(config.Formatters.XmlFormatter);

        // 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();

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

And my controller file that does the call has the proper CORS header as well: 我执行该调用的控制器文件也具有正确的CORS标头:

{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
public class RefurbItemsController : ApiController
{

    // GET: api/RefurbItems
    [Route("api/RefurbItems")]
    public HttpResponseMessage Get()
    {
        var items = RefurbItems.GetAllItems();
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, items);
        return response;
    }

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

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

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

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

It runs fine locally on both my dev machine and the server itself, however when I attempt to deploy it to the web and view the page I get the following error message: Origin not found in Access-Control-Allow-Origin header. 它在我的开发机和服务器本身上都可以在本地正常运行,但是当我尝试将其部署到Web并查看页面时,我收到以下错误消息:在Access-Control-Allow-Origin标头中找不到Origin。 XMLHttpRequest: Network Error 0x80070005, Access is denied. XMLHttpRequest:网络错误0x80070005,访问被拒绝。

If anyone can help out with some ideas or anything I am open to try I am new to asp.net 如果有人可以提供一些想法或任何帮助,我可以尝试我是asp.net的新手

Turned out the issue was that I had EnableCors located in 2 locations it was in both the class and the controller: 原来的问题是,我的EnableCors位于类和控制器的两个位置:

WebApiConfig.cs WebApiConfig.cs

    config.EnableCors();
    var json = config.Formatters.JsonFormatter;
    json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
    json.SerializerSettings.ContractResolver = new
        CamelCasePropertyNamesContractResolver();
    config.Formatters.Remove(config.Formatters.XmlFormatter);

Controller 调节器

[EnableCors(origins: "*", headers: "*", methods: "*")]  

All i had to do was delete the config.EnableCors() located in the WebApiConfig.cs and everything started working correctly. 我要做的就是删除位于WebApiConfig.cs中的config.EnableCors(),一切开始正常运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 4 / Asp.net Web API-不存在“ Access-Control-Allow-Origin”标头 - Angular 4 / Asp.net Web API - No 'Access-Control-Allow-Origin' header is present 在Access-Control-Allow-Origin标头中找不到源[域]。 ASP .NET CORE API MVC - Origin [domain] not found in Access-Control-Allow-Origin header. ASP .net CORE API mvc ASP.NET Core CORS WebAPI:没有 Access-Control-Allow-Origin 标头 - ASP.NET Core CORS WebAPI: no Access-Control-Allow-Origin header CORS asp.net 核心 webapi - 缺少 Access-Control-Allow-Origin 标头 - CORS asp.net core webapi - missing Access-Control-Allow-Origin header ASP.NET Core Web Api发送Access-Control-Allow-Origin:null CORS头和chrome是错误的,如何修复? - ASP.NET Core Web Api sending Access-Control-Allow-Origin: null CORS header and chrome is erroring, how to fix? ASP.NET Core CORS WebAPI:不保留 Access-Control-Allow-Origin 标头 - ASP.NET Core CORS WebAPI: persist no Access-Control-Allow-Origin header 在Asp.net Core 2.1的Angular 6中不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present Angular 6 with Asp.net Core 2.1 Access-Control-Allow-Origin 在标头请求 ASP.NET Web API 上出现两次 - Access-Control-Allow-Origin appearing twice on header request ASP.NET Web API Web API 2 CORS不存在“ Access-Control-Allow-Origin”标头 - web api 2 CORS No 'Access-Control-Allow-Origin' header is present ASP .NET 核心中 CORS 中的问题 - 响应中的“访问控制允许来源”header 的值不能是通配符 '* - Issue in CORS in ASP .NET Core - The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM