swagger doesn't work in blazor wasm

sblb 1,166 Reputation points
2022-11-21T20:14:19.667+00:00

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

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,403 questions
{count} votes