0

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:

    [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?

1
  • It would have been extremely helpful if you could show examples of "good" and "bad" HTTP response payloads (e.g. from Postman). Nevertheless - please look at Valuator's response below. Please "upvote" and/or "accept" if you find it helpful. Commented Mar 16, 2023 at 19:35

1 Answer 1

1

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wow I guess I never noticed that the name of the input parameter in the controller mattered. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.