Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use querystring within Blazor controller

I have the following code within my Blazor Server Project:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Mvc;
using RouteAttribute = Microsoft.AspNetCore.Mvc.RouteAttribute;

namespace ProjectName.Controllers
{
    [Route("/getfile")]
    [ApiController]
    [Authorize]
    public class ReturnFileController : ControllerBase
    {

        [Parameter]
        [SupplyParameterFromQuery]
        public int? fileId { get; set; }

        [HttpGet()]
        public async Task<IActionResult> GetFile()
        {
            File file = GetFileAsync(fileId);
        }
    }
}

But the fileId is not filled when I go to .../getfile?fileId=12

How do I use a querystring within my controller?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I don’t typically try to bind action parameters directly to class members. Have you tried:

[HttpGet()]
public async Task<IActionResult> GetFile([FromQuery] int? fileId)
{
    if (fileId == null) { 
        //do something 
    }

    File file = GetFileAsync(fileId.Value);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading