简体   繁体   English

Swagger UI和ASP.NET-找不到映射异常-无法访问Swagger UI页面

[英]Swagger UI and ASP.NET - Mapping not found exception - can't access swagger UI page

I have created some ASP.NET Web API project in Visual Studio 2017 (.NET Framework 4.6.1). 我已经在Visual Studio 2017(.NET Framework 4.6.1)中创建了一些ASP.NET Web API项目。

I enabled swagger UI using the SwaggerConfig.cs file. 我使用SwaggerConfig.cs文件启用了swagger UI。

Everything worked fine, I could access the UI via http://localhost:54408/swagger/ui/index/ . 一切正常,我可以通过http://localhost:54408/swagger/ui/index/来访问UI。

I wanted to go directly to this URL when the application starts (the default address was: http://localhost:54408 ), so I changed the Project Url value in the project properties ( Web tab) to http://localhost:54408/swagger/ui/index/ . 我想在应用程序启动时直接转到该URL(默认地址为: http://localhost:54408 ),因此我将项目属性(“ Web选项卡)中的“ Project Url值更改为http://localhost:54408/swagger/ui/index/

From that moment I cannot access the swagger UI page. 从那一刻起,我无法访问庞大的UI页面。 I tried to rollback my changes but it didn't help. 我试图回滚更改,但没有帮助。

Now, when I try to enter the swagger's UI page I'm getting the following exception: 现在,当我尝试进入swagger的UI页面时,出现以下异常:

<Error>
    <Message>An error has occurred.</Message>
    <ExceptionMessage>Mapping not found - index/</ExceptionMessage>
    <ExceptionType>Swashbuckle.SwaggerUi.AssetNotFound</ExceptionType>
    <StackTrace> at Swashbuckle.SwaggerUi.EmbeddedAssetProvider.GetAsset(String rootUrl, String path) at Swashbuckle.Application.SwaggerUiHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    </StackTrace>
</Error>

I did some research and found out that I should have deleted a .vs folder in the project folder. 我进行了一些研究,发现应该删除项目文件夹中的.vs文件夹。 It did not work. 这没用。 Any ideas on what else I can do? 关于我还能做什么的任何想法?

At this point I know that I should change the Start URL option in the project's properties, but now I am stuck with exception described. 至此,我知道我应该在项目的属性中更改“ Start URL选项,但是现在我陷入了异常描述中。

I had stumbled upon the same problem. 我偶然发现了同样的问题。 After much time I was not able to find an easy solution for this and the only way I could think of fixing this was actually just creating a new Project and copying all of the files into that new project. 花费了很多时间后,我无法找到一个简单的解决方案,而解决此问题的唯一方法实际上是创建一个新项目并将所有文件复制到该新项目中。 Deleting the old one, setting the startup project to the new one and done. 删除旧项目,将启动项目设置为新项目并完成。 Works like a charm. 奇迹般有效。

Very late response, but i think i just figured this one out. 反应很晚,但我想我只是想出了这一点。

Long answer: The problem is caused by adding the application root to be the swagger URL which as you pointed out causes a virtual directory to be created at that location, meaning that the swagger documentation cannot be loaded. 长答案:问题是由于将应用程序根目录添加为swagger URL,正如您所指出的那样,该URL导致在该位置创建虚拟目录,这意味着无法加载swagger文档。 When you load the application, dotnet also creates a http 301 redirect to add any missing "/" character onto the end of the URL in the root directory. 加载应用程序时,dotnet还会创建一个http 301重定向,以将任何缺少的“ /”字符添加到根目录中URL的末尾。 This redirect is cached client side so that even when you revert your changes and delete the .vs directory (which are both required), the browser is still trying to load the swagger from 'swagger/ui/index/' instead of 'swagger/ui/index'. 此重定向是在客户端缓存的,因此,即使您还原更改并删除.vs目录(这都是必需的),浏览器仍会尝试从'swagger / ui / index /'而不是'swagger / ui / index”。 You can fix this by disabling the cache in browser dev tools 您可以通过禁用浏览器开发工具中的缓存来解决此问题 禁用缓存 or by using a different browser. 或使用其他浏览器。

Short answer: To fix: 简短答案:解决方法:

  1. Revert root path of application 还原应用程序的根路径
  2. Delete .vs directory 删除.vs目录
  3. Disable browser cache temporary (can be re-enabled once it's loaded successfully) 暂时禁用浏览器缓存(加载成功后可以重新启用)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM