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

how to pass empty string in web api
how to pass null value in rest service
how to pass null value in query string
rest api null values
how to pass empty string in url
angular http params encoding
httpparams
how to pass query params 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.

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.

Null value getting passed as String in params · Issue #83 · mgonto , Here the Get method is not invoking since the value parameter is empty. 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?}")] Going with an empty string is a definitive no. Empty string still is a value, it is just empty. No value should be indicated using a construct which represents nothing, null. From API developer's point of view, there exist only two types of properties: required (these MUST have a value of their specific type and MUST NOT ever be empty),

I only have experience with asp.net web api but as far as i am aware having the string value as empty causes the routing to change. Since you don't have a route that takes only an int, your request never reaches your api. What you need to do is overload your Get method and make one that only takes an int.

Use empty string, null or remove empty property in API request , GitHub is home to over 50 million developers working together to host and review the variable before passing it into restangular and it shows true null (not string). function staffListCtrl($scope, Restangular, $stateParams){ /api/employees?​frank=null" chanakyachatterjee commented on Jul 6, 2018  Well yes. The documentation States that you'll get an exception of you try to convert an empty string. So you either check for empty string, or you can use int.TryParse() instead.

You entered the type of the variable shareType in the URL, and you are passing empty string for variable shareValue. Remove the :number after the variable shareType and assign a value to the variable shareValue.

HttpParams, As always, generalizations only get you so far. No value should be indicated using a construct which represents on startup and null is a possible string value passed back upon failure to bgusach 260○11 silver badge○66 bronze badges is clean, string type methods only return string s, never null. Important: the In-memory Web API module has nothing to do with HTTP in Angular. If you're just reading this tutorial to learn about HttpClient, you can skip over this step. . If you're coding along with this tutorial, stay here and add the In-memory Web API n

Validators, string | null : The first value of the given parameter, or null if the parameter is not present. getAll() link · mode_edit code. Retrieves all values for a parameter. GetMethod(String, Int32, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[]) Searches for the specified method whose parameters match the specified generic parameter count, argument types and modifiers, using the specified binding constraints and the specified calling convention.

Post a simple string value from AngularJS to .NET Web API, The minLength validator logic is also not invoked for values when their length property is 0 (for example in case of an empty string or an empty array), to support  ii) Need a recommended value to pass to the method (either empty string or null value) to stick with best coding practices. From the given answers, I got the best solution for my problem. The linked question which has been referred this question duplicate also provides info about handling null value, but not covered the first point I mentioned

AngularJS: API: $resource, Without the content-type header, I kept getting null passed in, even with the double-quotes. svašta • 5 years ago. what about providing the whole  The class String includes methods for examining individual characters, comparing strings, searching strings, extracting parts of strings, and for converting an entire string uppercase and lowercase. Strings are always defined inside double quotes ( "Abc" ), and characters are always defined inside single quotes ( 'A' ).

Comments
  • Can you please mention the error message you are getting?