question

sblb-6958 avatar image
0 Votes"
sblb-6958 asked AgaveJoe edited

swagger doesn't work in blazor wasm

Hi, I've an application blazor wasm .net core 6. I try to use swagger and I received the message below

 SwaggerGeneratorException: Ambiguous HTTP method for action - AppWeb.Server.Controllers.ServerMethodsController.MonthlyStats (AppWeb.Server). Actions require an explicit HttpMethod binding for Swagger/OpenAPI 3.0
 Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GenerateOperations(IEnumerable<ApiDescription> apiDescriptions, SchemaRepository schemaRepository)

Have you an idea why I receive this message and How I can fix it? because I need to use swagger.
Plus I installed swagger via nugget's package;

Apparently it comes from the controller ServerMethodsController.MonthlyStats .

Api

   [Route("api/[controller]/[action]")]
 public class ServerMethodsController : Controller
 {
     private readonly ApplicationDBContext context;
     private readonly int _currentYear = DateTime.Today.Year;
     private readonly int _currentMonth = DateTime.Today.Month;
               

     public ServerMethodsController(ApplicationDBContext context)
     {
         this.context = context;
     }
    
     public IActionResult MonthlyStats()
     {            
         int numberecr = context.Developers.Count();
        int calculN = context.Developers.Count(number => number.DateSolde.Value >  number.DateSoldePr.Value && number.Statut == "Close");
         int open = context.Developers.Count(a => a.DateCrea.Month == DateTime.Today.Month);
         int close = context.Developers.Count(a => (a.DateSolde.HasValue && a.DateSolde.Value.Month == DateTime.Today.Month) && (a.DateSolde.HasValue && a.DateSolde.Value.Year == _currentYear));
            
        double data = Math.Round(((double)calculN) / numberecr, 1);

         var stats = new
         {
             NombreECR = calculN,
             Ratio = data,
             EcrTotal = numberecr,
             OpenECR = open,
             CloseECR = close
         };
         return Ok(JsonSerializer.Serialize(stats, new JsonSerializerOptions 
         { 
             PropertyNamingPolicy = null
         }));
           
     }

Indicateur.razor.cs

  public partial class IndicateurComponent
     {
         [Inject]
         HttpClient? Http { get; set; }
    
         public async Task<Stats> MonthlyStats()
           {
             var response1 = await Http.SendAsync(new HttpRequestMessage(HttpMethod.Get, new Uri($"{UriHelper.BaseUri}api/servermethods/MonthlyStats")));
             return await response1.ReadAsync<Stats>();
           }
    
 ...
 }

Indicateur.razor.design.cs


 public partial class IndicateurComponent : ComponentBase
 {
     [Inject]
     protected NavigationManager UriHelper { get; set; }

     Stats _monthlyStats;
     protected Stats monthlyStats
     {
         get
         {
             return _monthlyStats;
         }
         set
         {
             if(!object.Equals(_monthlyStats, value))
             {
                 _monthlyStats = value;
                 InvokeAsync(() => { StateHasChanged(); });
             }
         }
     }
    protected async System.Threading.Tasks.Task Load()
       {
             monthlyStats = await MonthlyStats();



it's based on this link radzen-blazor


dotnet-aspnet-core-blazor
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The GitHub solution you've include is from a 3rd party. Contact Radzen support for assistance with their solution.

The Core SDK comes with templates for creating various projects. Creating a hosted Blazor WASM project is one line of code.

  dotnet new blazorwasm --hosted

Add the Swashbuckle.AspNetCore Nuget.

https://www.nuget.org/packages/Swashbuckle.AspNetCore

Update the program.cs file to include swagger.

 using Microsoft.AspNetCore.ResponseCompression;
    
 var builder = WebApplication.CreateBuilder(args);
    
 // Add services to the container.
    
 builder.Services.AddControllersWithViews();
 builder.Services.AddRazorPages();
    
 builder.Services.AddEndpointsApiExplorer();
 builder.Services.AddSwaggerGen();
    
    
 var app = builder.Build();
    
 // Configure the HTTP request pipeline.
 if (app.Environment.IsDevelopment())
 {
     app.UseWebAssemblyDebugging();
     app.UseSwagger();
     app.UseSwaggerUI();
 }
 else
 {
     app.UseExceptionHandler("/Error");
     // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
     app.UseHsts();
 }
    
 app.UseHttpsRedirection();
    
 app.UseBlazorFrameworkFiles();
 app.UseStaticFiles();
    
 app.UseRouting();
    
    
 app.MapRazorPages();
 app.MapControllers();
 app.MapFallbackToFile("index.html");
    
 app.Run();

Open the URL

 https://localhost:xxxx/swagger/index.html



0 Votes 0 ·

0 Answers