Search code examples
asp.net-coreasp.net-core-webapiblazor-server-side

ASP.NET Core Blazor API - FromQuery is always returning null


I have an API endpoint that takes a query string parameter, but the value returned is always null, even when a value is passed.

This is the method in my Blazor web app:

public async Employee GetEmployee(EmployeeDetails details)
{
    var qString = string.Empty;
    
    if (details != null)
    {
        if (details.EmployeeId != null)
        {
            qString += "empId=" + details.EmployeeId;
    
            var employee = await _httpClient.GetAsync($"api/EmployeeData/GetData?{qString}");
    
            return employee;
        }
    }
}

I have the following endpoint in the employee data controller in the API:

public async Task<ActionResult<Employee>> GetData([FromQuery] string? Id)
{
    // The value of query string Id is always null here.
}

Am I missing something? Could someone point me in the correct direction, please?

Thanks in advance


Solution

  • So as an answer.

    Name your query parameter the same way, like you have it in your API:

    public async Employee GetEmployee(EmployeeDetails details)
    {
        if (details != null)
        {
            if (details.EmployeeId != null)
            {
                var qString = "Id=" + details.EmployeeId;
        
                var employee = await _httpClient
                     .GetAsync($"api/EmployeeData/GetData?{qString}");
        
                return employee;
            }
        }
    }