【问题标题】:web api routes not working with CORS enabled.web api 路由在启用 CORS 的情况下不起作用。
【发布时间】:2015-07-18 14:30:50
【问题描述】:

我在我的 web api 应用程序中启用了 CORS。我有 API 控制器,它既有经典的 REST 函数名称,如 Get() 和 Get(string id),也有一个具有自定义函数名称的控制器,例如 [HttpGet] GetSomeThing()

我有这样配置的路线

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

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

通过这种安排,当我尝试使用经典 REST 功能调用控制器时,出现错误 No 'Access-Control-Allow-Origin' 标头。

例如/api/控制器

如果我在没有操作的路由之后使用带有操作的路由,它会在使用自定义函数名称的控制器调用时给我同样的错误。

例如/api/控制器/函数

请注意我有

    [EnableCorsAttribute("http://localhost:xxxx", "*", "*")]

两个控制器的属性。这些调用是通过角度应用程序进行的。

善意的建议。

【问题讨论】:

    标签: asp.net-web-api cors asp.net-mvc-routing


    【解决方案1】:

    我遇到了这个问题,我发现让 cors 工作是一场噩梦。

    最后对我的修复是删除我的应用程序中对 cors 的所有引用,并在 WebAPIConfig 中仅添加一行

     public static void Register(HttpConfiguration config)
        {
            config.EnableCors(new EnableCorsAttribute("*", "*", "GET, POST, OPTIONS, PUT, DELETE, TOKEN"));
    
            // Web API routes
            config.MapHttpAttributeRoutes();
    
            config.Routes.MapHttpRoute(
                name: "ActionApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
    
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "rest/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
    
            var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    
            var json = config.Formatters.JsonFormatter;
            json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
            config.Formatters.Remove(config.Formatters.XmlFormatter);
    
        }
    

    我还从我的 web.config 中删除了自定义标头。希望这可以帮助其他面临 cors 问题的人。

    【讨论】:

      【解决方案2】:

      有同样的问题。通过将 CORS 标头直接添加到 web.config 来修复它。无需进行其他更改。

        <system.webServer>
          <httpProtocol>
            <customHeaders>
              <add name="Access-Control-Allow-Origin" value="*" />
              <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS" />
              <add name="Access-Control-Allow-Headers" value="content-type, accept, SOAPAction, origin" />
            </customHeaders>
          </httpProtocol>
        </system.webServer>
      

      此外,如果您通过 NuGet 安装了 CORS,并且使用的是以前版本的 WebAPI,那么您可能需要将其卸载。 CORS 将更新某些可能导致兼容性问题的核心 WebAPI 程序集,即路由不起作用。

      当然,您可能希望通过将 Access-Control-Allow-Origin 值中的“*”替换为您要允许的 URL 来限制允许的来源。

      【讨论】:

        猜你喜欢
        • 2018-10-30
        • 2018-10-10
        • 2014-03-12
        • 2016-03-05
        • 2018-01-01
        • 2013-06-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多