.Net Framework Swagger Route Prefix - c#

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.

Related

How to configure Swashbuckle to read/write Swagger.json from/to App_Data folder?

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 =>
{
});
}
}

Swagger documentation does not update after deployment using Azure release pipeline

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.

Swagger not detecting controllers inside feature folder

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

No constructor for type SwaggerGenerator can be instantiated using services from the service container and default values

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

.NET Core API ToDoAPI does not work out of the box

I am trying to create a new API in VS Code. I am following the instructions on this page from MS:
https://learn.microsoft.com/en-us/aspnet/core/tutorials/web-api-vsc
The directions to get started are simple. With the Core SDK (2.1.4) installed, in the console I run this:
mkdir TodoApi
cd TodoApi
dotnet new webapi
That creates a new project which I should be able to run from either VS or VS Code. When I try running from either however, I get an error message that the "localhost:/5000" cannot be found.
I'm assuming this is a bug in the code the SDK generated but I'm not C# savvy. Has anyone else had this problem? Is there a resolution?
If you mean that the browser has a white screen with "localhost:/5000 cannot be found" then yes, this is expected behaviour, the default launch settings for a solution is to go to the route of the project "/" but a web api does not fufill this contract (does not have an action for it).
If you want to test your web api go to
http://localhost:5000/api/Values/Get
How do I know this?
In the web api project there is a folder called Controllers in there there is a class called ValuesController in that file you can see that the class has the attribute
[Route("api/[controller]")]
Please read here for more information on attribute routing
https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing
I'll just state that the default way to get to this controller is /api/Values (removing the word Controller as is standard) and then call an action (could use any but I used get).
There are ways to override the default base url when you run your application, the settings that are used to launch and build your application are all under the ./vscode/ directory of your project, maybe have a play around.
Hope this helps.
There's a bug... but not with the SDK, its with the tutorial - when you use dotnet new webapi it creates the project correctly, but likely using different ports.
If you go to the Properties\launchsettings.json file you will see what port you should be using. e.g.
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:53096/",
"sslPort": 0
}
},
Then you can navigate to http://localhost:53096/api/values to see it work.
Note that the root http://localhost:[your port]/will always return Not Found because that webapi template does not not have any routing setup for that.
The rest of the tutorial shouldn't have any other hiccups.

Categories