I'm trying to use Swagger in my projects, but its not detecting my controllers.
This is the configuration of the swagger in my Startup.cs
...ConfigureServices method
services.AddSwaggerGen(opts =>
{
opts.SwaggerDoc("v1", new Info { Title = "API", Version = "v1" });
});
...Configure method
app.UseSwagger();
app.UseSwaggerUI(opts =>
{
opts.SwaggerEndpoint("/swagger/v1/swagger.json", "API");
opts.RoutePrefix = string.Empty;
});
this is the structure of my code
If i have a folder with the name "Controllers" and i have a controller inside, the swagger find the controller and print it out in the swagger page, but if the controller is inside the feature folder, it does not find it.
here is the full code
Swashbuckle uses Microsoft.AspNetCore.Mvc.ApiExplorer, so if asp net core finds your controllers Swashbuckle will too. It doesn't care what folders you use.
If your controllers are in your startup project there shouldn't be anything you need to do. Just follow available tutorials. Refer to Get started with Swashbuckle and ASP.NET Core
Related
I am implementing Swagger on a .NET 4.8 WebApi, I used Nuget Package Manager to install Swashbuckle 5.6.0.
I'm getting the following error when trying to load http://localhost:53374/swagger/ui/index
HTTP Error 403.14 - Forbidden The Web server is configured to not list
the contents of this directory.
Another kind developer has mentioned in order for this to load, it first needs to read/write to a swagger.json file.
I was thinking maybe the App_Data folder could be a good match for this, instead of whatever default directory Swagger is trying access. It is what App_Data is basically for ins't it?
What is the default directory that Swagger is trying to write to? The WebAPI root?
What configuration changes do I need to do to Swashbuckle 5.6.0 to accomplish this?
swagger.config (stock standard):
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
})
.EnableSwaggerUi(c =>
{
});
}
}
I am using Swashbuckle and swagger to document my .NET Core (3.1) API project.
The documentation is updated fine at the swagger endpoint, when I use the publish functionality in Visual Studio 2019 16.4.2.
However using the release pipeline in Azure does achieve the same.
Using swagger gen like this:
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "SqlViewService",
Version = "v1",
Description = "Alter views..."
});
});
I initially started adding the endpoint in the startup.cs class like this:
app.UseSwagger().UseSwaggerUI(s =>
{
s.SwaggerEndpoint("/swagger/v1/swagger.json", "SqlViewService v1");
});
And after searching a few articles and questions I have tried this:
app.UseSwagger().UseSwaggerUI(s =>
{
s.SwaggerEndpoint("/swagger/v1/swagger.json", "SqlViewService v1");
s.RoutePrefix = string.Empty;
});
And also this, adding current directory to the path:
app.UseSwagger().UseSwaggerUI(s =>
{
s.SwaggerEndpoint("./swagger/v1/swagger.json", "SqlViewService v1");
});
According to this article: https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-3.1&tabs=visual-studio
None of the tries have worked. Does anybody have a good suggestion or actual solution?
I had to add the following with in the Startup.cs class in the Configure method:
s.RoutePrefix = "swagger";
In this section:
app.UseSwagger().UseSwaggerUI(s =>
{
s.SwaggerEndpoint("/swagger/v1/swagger.json", "SqlViewService v1");
s.RoutePrefix = "swagger";
});
To solve page not found which suddenly started appearing.
Which prevented me from seeing whether the changes I made in my release pipeline was working, once that was solved.
The path to the folder used for deployment was:
And as my folder path for files to deploy was wrong I was basically deploying into a folder within old application files, so the release was never "picked up" by IIS.
I have .Net framework project with WepApi 2. I'm trying to use Swagger. My SwaggerConfig.cs file at AppStart folder include this code block:
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c => { c.SingleApiVersion("v1", "CustomerHelper.API");})
.EnableSwaggerUi(c => { });
At first start, my route is like this: http://localhost:60151/ and I got HTTP Error 403.14 - Forbidden.
When I change my route to http://localhost:60151/swagger/ui/index I can reach and use Swagger interface.
How can see directly Swagger interface at the beginning?
Try set startup URL of your project. Right click project -> Properties -> Web -> specific page.
set it to swagger should be fine.
I'm trying to add Swagger to my project. The error received is as follows.
No constructor for type 'Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator' can be instantiated using services from the service container and default values.
Since I haven't changed anything in Swagger binaries themselves, just installed the packages Swashbuckle.AspNetCore and Swashbuckle.AspNetCore.Swagger (both in version 4.0.1) I'm assuming that it's about the configuration. Following the suggestion here, I've set up the config shown below.
services.AddSwaggerGen(_ =>
{
_.SwaggerDoc("v1", new Info { Version = "v1", Title = "My API" });
});
app.UseSwagger();
app.UseSwaggerUI(_ => { _.SwaggerEndpoint("/swagger/v1/swagger.json", "API docs"); });
I'm not sure if I'm missing a package, if one of those I have is the wrong version or if the set config I'm providing isn't sufficient.
As the asker noted in a comment, what solved this for me was adding in a reference to Api Explorer in ConfigureServices.
Specifically the line required was:
services.AddMvcCore()
.AddApiExplorer();
You should register services.AddMvc(); in IServiceCollection, and configure app.UseMvc() in IApplicationBuilder in your application's Startup.cs
I am able to integrate the Swagge UI in my web api using Swashbuckle. I also want to explore the swagger codegen feature. Can somebody help in - how I can integrate swagger codegen into my web api project? Or do I need to download any tool? I want to be able it to host the codegen and pass the json/raml form specs to generate client in .net core.
I am not able to find enough docs on above.
EDIT : I want to know how I can host codegen in my WEBAPI.
Thanks!
Now, you can use Nswag. There are several code generator utilities - UI, Console, msbuild.
You should install "Swashbuckle.AspNetCore.Swagger" nuget package by right click your project and click manage nuget packages.
Then you should add these codes into your project startup place eg. Program.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});
}
public void Configure(IApplicationBuilder app)
{
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
app.UseMvc();
}
It seems like you just want to generate C# from the OpenApi specification (your Swagger implementation provides the input) of your API.
To generate code (e.g. C#) from the OpenApi spec file of your API, you can do something like this:
java -jar .\openapi-generator-cli-5.0.0-beta3.jar generate -i https://localhost:xxxx/api/v1/swagger.json -g csharp
You have to download the OpenApi Generator Jar. Alternatively you can upload your code to a web generator. But I would always run this locally; you never know where your code ends up.