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?
>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);
}