简体   繁体   中英

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).

I enabled swagger UI using the SwaggerConfig.cs file.

Everything worked fine, I could access the UI via http://localhost:54408/swagger/ui/index/ .

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/ .

From that moment I cannot access the swagger UI page. 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:

<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. 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.

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. 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. 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'. 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
  3. Disable browser cache temporary (can be re-enabled once it's loaded successfully)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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