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

.NET core controller not receiving correct input from query string

I have an Angular client making requests to a .NET core API. There are two functions that are nearly identical, one works, and the other does not. I can not figure out what the difference is.

Here are the two functions on the client side using the angular/common/httpclient:

getInstitutionSettings(institutionId: string): Promise<any | undefined> {
    return this.http
            .get<InstitutionSettingsModel>('https://localhost:7205/api/v1/Settings/get', {
                params: {
                    id: institutionId,
                },
            })
            .toPromise();
}

getAllInstitutionSettings(): Promise<any | undefined> {
    return this.http
            .get<InstitutionSettingsModel[]>('https://localhost:7205/api/v1/Settings/getAll', {
                params: {
                    id: this.tempTestOrgId,
                },
            })
            .toPromise();
}

Here are the C# controller methods that receive the requests:

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

    [HttpGet]
    [Route("get")]
    [EnableCors]
    [Produces("application/json")]
    [SwaggerResponse(StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<IActionResult> Get([FromQuery] Guid instId)
    {
        SettingsReadRequestModel req = new SettingsReadRequestModel()
        {
            Id = instId,
            JsonWebToken = this.TestJwt,
            CancellationToken = CancellationToken.None
        };

        if (instId != Guid.Empty)
        {
            SettingsGetResponseModel res = await this.Service.GetSettingsAsync(req);
            return this.Ok(res.Institution);
        }
        else
        {
            return this.BadRequest();
        }
    }

    [HttpGet]
    [Route("getall")]
    [EnableCors]
    [Produces("application/json")]
    [SwaggerResponse(StatusCodes.Status200OK)]
    public async Task<IActionResult> GetAll([FromQuery] Guid id)
    {
        SettingsReadRequestModel req = new SettingsReadRequestModel()
        {
            Id = id,
            JsonWebToken = this.TestJwt,
            CancellationToken = CancellationToken.None
        };

        SettingsGetAllResponseModel res = await this.Service.GetAllSettingsAsync(req);

        if (id != Guid.Empty)
        {
            return this.Ok(res.Institutions);
        }
        else
        {
            return this.BadRequest();
        }
    }

The differences in the functions are a matter of what scope the input GUID is. GetAll() accepts a parent identifier that returns a list and this is the function that works as expected

Get() accepts an identifier for a specific entity and returns only one entity. This method never receives the correct input.

In both cases the two functions in the client send a valid GUID in string format in the query string. I have verified this in the debugger. And in both controller methods, the arguments have the [FromQuery] attribute and are expecting a GUID.

EDIT: I think it might be worth noting that in the client, the class variable this.tempTestOrgId is hard coded and is implicitly typed. It has no type identifier

private tempTestOrgId = 'e41093cb-1f42-4f70-802b-64503e24a2b6';

In GetAll() the function receives the correct GUID from the client
In Get() the function always receives an Empty GUID (all 0’s) even though the client passes in a correctly formatted GUID as a string.

What am I missing? Why does one function work as expected and the other does not?

>Solution :

Your Get endpoint expects a parameter named instId, not id. Change your calling function to:

getInstitutionSettings(institutionId: string): Promise<any | undefined> {
    return this.http
            .get<InstitutionSettingsModel>('https://localhost:7205/api/v1/Settings/get', {
                params: {
                    instId: institutionId,
                },
            })
            .toPromise();
}
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