I created an ASP.NET Core 5 Web App for “API” (similar to MVC) with the OpenAPI (aka Swagger) option turned on.
To note: I have nearly 2 decades experience with .NET but this is my first real-world dive into .NET Core/5, so I’m skipping the whole “learning from first principals” and figuring it out as I go off the back of what I already know.
In this case I’m setting up a new project to move some part of an existing ASP.NET 4.6 MVC app into it.
It was all working fine until I wanted to add an OnActionExecuting()
override to the Controller, from the IActionFilter
interface.
Once I added the interface and the 2 required method the calls to the actual controller, calling the endpoints worked fine. But F5 debug runs of the project, which loaded the https://localhost:xxxxx/swagger/index.html page, would produce the error:
Failed to load API definition.
Errors
Fetch errorundefined /swagger/v1/swagger.json
Which in typical 3rd party framework fashion is fucking useless for consuming and gives use no useful information to work with.
So off to StackOverflow I go.
After a search for OnActionExecuting Fetch errorundefined /swagger/v1/swagger.json
I came across this page: https://stackoverflow.com/questions/48450262/asp-net-core-swashbuckle-not-creating-swagger-json-file, specifically the answer https://stackoverflow.com/a/53632914/115704 which mentioned [NoAction]
. Things then clicked for me.
The [NoAction]
attribute on the 2 interface methods didn’t work for me, but after a bit more searching I found [NonAction]
and which solved the problem.
Then I was back up and working with Swagger again.
Screenshots
Code before the fix
(Noting the [NonAction] is not impmented)
The error from Swagger
Honestly, how fucking useless is this an an error? About as good as the classic “an unexpected error occurred” (to which I always exclaim: “no shit, Sherlock!” Like any developer actually expects an error).
Code after the Fix
[NonAction] attributes have been implemented.
Swagger after the fix
And look, Swagger works again.
At the end of the day this was a couple of hours of my night lost.
Partly my fault for implementing a new version of .NET without doing the obligatory 40 hours of training. Also partly Swaggers fault because, well, it pissed me off for not giving a more detailed error message, especially in a development environment.