Requirements: Scalar is quite powerful and can automatically generate HTTP call code in different languages from API documentation, using Scalar instead of Swagger to display API documentation.
After the release of .NET 9 ASP.NET the official Core team announced that Swashbuckle.AspNetCore (a project providing the Swagger tool for the ASP.NET Core API) would be removed from the ASP.NET Core Web API template. This means that in the future, when we create Web API projects, Swagger API documentation will no longer be automatically generated. So today, let's try using Scalar for interactive API documentation in our EasySQLite .NET 9 project.
Link:The hyperlink login is visible.
The reasons for Swagger's removal can be summarized as follows:
- Swashbuckle Poor Maintenance: The Swashbuckle project is no longer actively maintained by community owners, many issues remain unresolved, and an official version compatible with .NET 8 has not been released.
- Turning to Microsoft.AspNetCore.OpenApi: The ASP.NET Core team will enhance Microsoft.AspNetCore.OpenApi's capabilities to replace Swashbuckle and enable OpenAPI documentation generation.
- Existing alternatives: Besides Swashbuckle, other projects like NSwag support OpenAPI documentation generation and client/server code generation, allowing developers to choose the appropriate solution based on project needs.
- Enhanced built-in API support: Starting from ASP.NET Core 3.1, the framework has provided metadata support such as ApiExplorer. Combined with Visual Studio and Visual Studio Code's built-in support for .http files, the API testing and debugging experience is even better.
- Promoting OpenAPI as a core component: ASP.NET Core team plans to further enhance OpenAPI integration in .NET 9, making it a core component focused on generating JSON-formatted OpenAPI documentation.
Scalar
Scalar is a powerful and easy-to-use API client and documentation generation tool suitable for API projects of all sizes, supporting multiple programming languages and platforms.
Open Source Address:The hyperlink login is visible.
ASP.NET Core integrates Scalar
Integrated documentation:The hyperlink login is visible.
Create a ASP.NET Core 9 project, referenced as follows:
Add services and scalar middleware in Program.cs, with the code as follows:
Edit WeatherForecastController.cs controller, code as follows:
Launch the project, open your browser, and visit:http://localhost:5088/api-docs, as shown in the figure below:
You can choose to generate specific call code for HTTP libraries in different languages. Taking the C# HttpClient library as an example, the generated code is as follows:
It also supports online interface testing. Click the Test Request button, fill in the post data, then click send to send the request, as shown below:
Customize the default HTTP client. For C# teams, we want to open the API documentation interface, which automatically displays the C# HttpClient call code. If you don't want to manually select every first time, configure as follows:
Delete the Ask AI feature and remove the Ask AI Agent button. This feature feels a bit redundant. The code is as follows:
As shown below:
After deletion, as shown below:
Similarly, disable MCP functionality, code as follows:
The complete configuration is as follows:
Add JWT authentication and carry the request headerBearer Token
Add security requirements to the entire OpenAPI documentation, code as follows:
As shown below:
OpenID Connect (OIDC):The hyperlink login is visible.
Reference:
The hyperlink login is visible.
The hyperlink login is visible. |