Search code examples
parametersentity-framework-coreasp.net-core-webapiblazor-webassembly

passing IEnumerable<T> to API method in Blazor WASM


please, how can I pass an IEnumerable parameter using Http.GetFromJsonAsync in a Blazor WASM project? This is my code in razor component:

IEnumerable<WAGPiattValidePerUtente> piattValidePerUtente;

dipendenti = await Http.GetFromJsonAsync<IEnumerable<WAGDipendentiAttivitaNGiorniPrec>>($"WAGDipendentiAttivitaNGiorniPrecService/ByDaySottrDataPiattaforme/{(short)7}/{dataRif.ToString("yyyyMMdd")}/{piattValidePerUtente}");

In my API controller the code is:

        [HttpGet("ByDaySottrDataPiattaforme/{giorniDaSottrarre}/{dataRif}/{piattaforme}")]
    public async Task<ActionResult<IEnumerable<WAGDipendentiAttivitaNGiorniPrec>>> GetWAGDipendentiPiattAttivitaNGiorniPrec(short giorniDaSottrarre, string dataRif, IEnumerable<WAGDipendentiAttivitaNGiorniPrec> piattaforme)
    {
      //call a store procedure...
    }

When the method is called I have this error: Failed to load resource: the server responded with a status of 415 () Unsupported Media Type

Can someone help me, please?


Solution

  • how can I pass an IEnumerable parameter using Http.GetFromJsonAsync in a Blazor WASM project?

    Failed to load resource: the server responded with a status of 415 () Unsupported Media Type

    Normally we use route data and query string values to pass simple types data.

    To achieve your requirement of passing an IEnumerable parameter to action method through route data, you can try to implement and use a custom model binder, like below.

    public class MyModelBinder : IModelBinder
    {
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException(nameof(bindingContext));
            }
    
            // ...
            // implement it based on your actual requirement
            // code logic here
            // ...
    
            var model = new List<WAGPiattValidePerUtente>();
    
            if (bindingContext.ValueProvider.GetValue("piattaforme").FirstOrDefault() != null)
            {
                model = JsonSerializer.Deserialize<List<WAGPiattValidePerUtente>>(bindingContext.ValueProvider.GetValue("piattaforme").FirstOrDefault());
            }
    
    
            bindingContext.Result = ModelBindingResult.Success(model);
            return Task.CompletedTask;
        }
    } 
    

    Apply it on action parameter

    [HttpGet("ByDaySottrDataPiattaforme/{giorniDaSottrarre}/{dataRif}/{piattaforme}")]
    public async Task<ActionResult<IEnumerable<WAGDipendentiAttivitaNGiorniPrec>>> GetWAGDipendentiPiattAttivitaNGiorniPrec(short giorniDaSottrarre, string dataRif,
        [ModelBinder(BinderType = typeof(MyModelBinder))]
    IEnumerable<WAGPiattValidePerUtente> piattaforme)
    {
        //call a store procedure...
    

    Make request from client app

    var piattValidePerUtente_data = Uri.EscapeDataString(JsonSerializer.Serialize(piattValidePerUtente));
    
    dipendenti = await Http.GetFromJsonAsync<IEnumerable<WAGDipendentiAttivitaNGiorniPrec>>($"WAGDipendentiAttivitaNGiorniPrecService/ByDaySottrDataPiattaforme/{(short)7}/{dataRif.ToString("yyyyMMdd")}/{piattValidePerUtente_data}");
    

    Test Result

    enter image description here