0

In TypesScript, I have an interface defined as

export interface ClientReportParm {
 StartDate?: Date;
 EndDate?: Date;
}

I have a method that calls an API endpoint defined as

ClientReport(parm: ClientReportParm) {
 this.searchParams = new HttpParams();
 this.searchParams = this.searchParams.append('parm', JSON.stringify(parm));
 return this.http.get('http://localhost:5000/api/service/retclientreport', { params: 
 this.searchParams });
}

In C# However, I need to translate the above method code to a C# equivalent for ASP.NET Core. The line of code I need to use is

http.get(this.baseUrl + 'retclientreport', { params: this.searchParams });

// I have tried to do the following

// This is where I need to pass the stringified json object 
var response = await client.GetAsync('http://localhost:5000/api/service/retclientreport', 
param??); // How do I pass the equivalent json object here???
jps
  • 20,041
  • 15
  • 75
  • 79
  • do you want a pass query string something like this? http://localhost:5000/api/service/retclientreport?startDate=xxxxx&endDate=yyyyy ? – Hadi R. Dec 11 '21 at 18:52
  • I need to pass an object as a parameter to a GetAsync method without necessarily constructing a query string. Is it possible? – Francis Irasa Dec 11 '21 at 18:57
  • unfortunately not, in Get methods you are just allowed to pass query string in the URL unless you pass all the stringified object as one parameter in one parameter in the query string and then read it in your c# method and construct your object there (which is dirty and not recommended), in Post methods you are allowed to send an object in the body of the method. – Hadi R. Dec 11 '21 at 19:01
  • Passing one stringified object as one parameter would work for my case because the API already has the mechanism to convert a stringified object into an actual object. Could you elaborate why it is not recommended? The API has the signature like this: APIGetMethod([FromQuery] string parm) – Francis Irasa Dec 11 '21 at 19:09
  • The APIGetMethod body then deserilizes the paramater object as JsonConvert.DeserializeObject(parm); – Francis Irasa Dec 11 '21 at 19:14
  • well, as I have mentioned it is not clean and not a common way to pass data by Get method, if you pass query strings and fetch it in the parameters of your c# API method you are going to reduce one step in your method. I have just told according to the standards, on the other hand you are allowed to implement as you wish. – Hadi R. Dec 11 '21 at 19:17

0 Answers0