Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API Get Method not invoking when passing an integer value with empty string in Angular 6

In my angular application, I am getting an error when invoking an API method from angular. I have to pass two parameters. First one an integer value and second one string value. it is optional.

Please see the below code snippet (Typescript)

let id:number = 5;
let value: string = "";

this.http.get<string[]>(this.appService.baseUrl + 'api/File/' + id + "/" + value)

In Controller:

[HttpGet("{id:int}/value")]
[ResponseCache(NoStore = true)]
public async Task<IActionResult> Get(int id, string value) { }

Here the Get method is not invoking since the value parameter is empty.

like image 857
Krishnan Avatar asked Mar 05 '23 20:03

Krishnan


1 Answers

In your example, you're building this URL:

/api/File/5

However, your controller expects the following:

/api/File/5/value

If you want value here to be optional and to be placed into the value parameter (string value), you can adjust your HttpGet attribute, like this:

[HttpGet("{id:int}/{value?}")]

This makes value optional and is a placeholder for whatever gets passed in.

like image 124
Kirk Larkin Avatar answered Mar 08 '23 12:03

Kirk Larkin