Reference Api in c#

Viewed 260

I created an api for CRUD operations on Db and then through Swagger Editor I generated a Client Api file.

On my project I made the reference to the client file (Let's suppose it is called swagFile) previously generated.

if I type "swagFile" in my project blazor intellisense shows me the various methods of my api, but they don't work because I have to register a service in Startup.cs first.

i have no idea how to initialize the service and start using my api's methods. and I don't know if you need to do the other steps first.

Sorry for my bad English.

1 Answers

As Michael Mairegger mentioned in his comment, there is a good description provided by Microsoft.

The most important part is that you have added a minimum configuration in Startup.cs like the following example (assuming it is using Swashbuckle.AspNetCore.SwaggerGen):

Define at class level

const string SwaggerTitle = "My.Api"; // name of your project
const string SwaggerVersion = "v1";

Configure the services as follows:

public void ConfigureServices(IServiceCollection services)
{   
    // services.AddDbContext<TodoContext>(opt => opt.UseInMemoryDatabase("TodoList"));
    services.AddControllers();

    // Register the Swagger generator
    // without authentication/authorization, this would be just: services.AddSwaggerGen();
    services.AddSwaggerGen(options =>
    {
        //Defines a document for one specific version, in the future we could have multiple documents depending on api versioning
        options.SwaggerDoc(SwaggerVersion, new OpenApiInfo { Title = SwaggerTitle, Version = SwaggerVersion });
        
        // This will add XML comments to Swagger 
        // (enable in Build/Output of the API project properties the option "XML documentation file for all configurations")  
        options.IncludeXmlComments(System.IO.Path.Combine(AppContext.BaseDirectory, $"{SwaggerTitle}.xml"));            
    });
}

And the Configure method should be like:

public void Configure(IApplicationBuilder app)
{
    // Enable middleware to serve generated Swagger as a JSON endpoint.
    app.UseSwagger(c =>
    {
        c.SerializeAsV2 = true;
        //c.RouteTemplate = "swagger/v1/swagger.json";
    });

    // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
    // specifying the Swagger JSON endpoint.
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", $"My API {SwaggerVersion}");
        c.RoutePrefix = string.Empty;
    });

    app.UseRouting();
    
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

There are much more options available, as described in the link above, but that'll be the starting point. Try it with that first, and then experiment with further settings from the article.

Note that you need to set up your project's properties to make the compiler generate an .XML file containing your XML comments (from the controller source code), which is then being used by Swagger.

In Visual Studio 2022, you can find it here in the project properties (Build / Output): XML File Gen

Important:

  • Make sure the option "Generate a file containing API documentation" (Documentation file) is enabled.

  • "InsertYourProjectPath": Use the base path for your project here. Usually, once you enabled XML file creation, the path suggested is already the right one - you just need to change the name of the XML file just as you named it in SwaggerTitle appended with .xml. If you're not sure, place a breakpoint inside .AddSwaggerGen and inspect AppContext.BaseDirectory.

Related