尽管启用了cors,Asp.NET WebAPI仍然无法访问 [英] Unable to access swagger despite cors enabled Asp.NET WebAPI

查看:151
本文介绍了尽管启用了cors,Asp.NET WebAPI仍然无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WebAPI-.NET Framework 4.6.2我通过招摇访问webapi时遇到以下错误:

I have a WebAPI - .NET framework 4.6.2 I am getting below error on the accessing the webapi via swagger:

无法从服务器读取.它可能没有适当的访问控制来源设置.

Can't read from server. It may not have the appropriate access-control-origin settings.

我的代码中已经有CORS我想知道会发生什么

I have already CORS in my code I wonder what is going worng

WebAPIConfig.cs

WebAPIConfig.cs

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {               
       config.EnableCors();
    }
}

GlobalAsax.cs

GlobalAsax.cs

protected void Application_Start()
{
  RouteTable.Routes.MapOwinPath("swagger", app =>
  {
    app.UseSwaggerUi(typeof(WebApiApplication).Assembly, new SwaggerUiSettings
    {
      MiddlewareBasePath = "/swagger",
      ShowRequestHeaders = true,
      DefaultUrlTemplate = "{version}/{controller}/{action}/{id}",
      AddMissingPathParameters = true,
      Version = "v1"
    });
  });
  IModule module = TfmsModule.GetModule(typeof(MyBCModule), "My");
  module.StartModule();
  Tracer.Info("MyAPI.Application_Start: MyBCModule(\"MyAPI\").StartModule()");
  GlobalConfiguration.Configure(WebApiConfig.Register);
}

我在控制器的开头添加了以下行

I have added below line at the start of my controller

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

我可以寻求帮助吗?

推荐答案

我假设您正在使用Swashbuckle.如果不是这种情况,那么我将删除该帖子!

I'm assuming you are using Swashbuckle. If this is not the case then I'll delete the post!

错误:

无法从服务器读取.它可能没有适当的访问控制来源设置

Can't read from server. It may not have the appropriate access-control-origin settings

如果根URL与传入请求的URL不匹配,则在请求Swagger UI时抛出

.根URL用于通过UI页面获取Swagger文档,因此,如果根URL和Swagger UI请求的URL不同,则会遇到CORS错误.根据Swashbuckle的注释:

is thrown when requesting the Swagger UI if the root URL does not match that of the incoming request. The root URL is used to fetch the Swagger docs by the UI page so if the root URL and the URL of the Swagger UI request differ then you hit a CORS error. As per the Swashbuckle notes:

默认情况下,服务根网址是从用于访问文档.但是,在某些情况下(例如代理和负载平衡的环境).你可以通过提供您自己的代码来确定根目录来解决此问题网址.

By default, the service root url is inferred from the request used to access the docs. However, there may be situations (e.g. proxy and load-balanced environments) where this does not resolve correctly. You can workaround this by providing your own code to determine the root URL.

因此,在某些情况下,您可能会通过代理进入,并且根URL设置不正确.您可以通过查看大页页面源代码并查找以下行来查看正在解析的根URL:

So there may be instances where you are coming in via a proxy and the root URL is not being set correctly. You can see the root URL that is being resolved by viewing the swagger page source and looking for the line:

window.swashbuckleConfig = {
    rootUrl: '<THE_RESOLVED_ROOT_URL>', ...

如果这与您的请求URL不同,则会抛出您所看到的错误.要解决此问题,请在您的请求中使用根URL或将根URL设置为您在SwaggerConfig.cs文件中请求的根URL:

If this is different from your request URL the error you are seeing will be thrown. To fix this either use the root URL in your request or set the root URL to the one you are requesting in the SwaggerConfig.cs file:

c.RootUrl(req => GetRootUrlFromAppConfig());

...其中 GetRootUrlFromAppConfig 是返回根URL字符串的方法.

...where GetRootUrlFromAppConfig is a method returning the root URL string.

这篇关于尽管启用了cors,Asp.NET WebAPI仍然无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆