Base solution for your next web application
Open Closed

How to pass object in method to call service API ? #3863


User avatar
0
manojreddy created

I have defined a method defined in AppService file, Signature of method is

public PagedResultDto<FetchData> GetSearchData(FetchDataInput searchInput)

. I'm calling this method from Angular code, but

service-proxoes.ts

file has generated method in which I need to pass all the parameters one by one.

public class FetchDataInput: PagedAndSortedInputDto, IShouldNormalize
{
    public int DataLevel { get; set; }
    public int DataType { get; set; }
    public string DataCode { get; set; }
    public string DescLong { get; set; }
    public string LanguageCode { get; set; }
    public string DataParent { get; set; }

    public void Normalize()
    {
        if (string.IsNullOrEmpty(Sorting))
        {
            Sorting = "DataCode";
        }
    }

}

Service-Proxies.ts file:

getSearchData(dataLevel: number, dataType: number, dataCode: string, descLong: string, languageCode: string, dataParent: string, sorting: string, maxResultCount: number, skipCount: number): Observable<PagedResultDtoOfFetchData> {

So I have to call the getSearchData method by the following way.

this._dataService.getSearchData(AppConsts.dataLevelType, undefined, undefined,
        undefined, this.currentLanguage.name, undefined, undefined, undefined, undefined).subscribe((result) => {
           //result.items;
        });

So I have to pass all the parameters, but if let's say there are 100 parameters, it will be error prone and not good programming style. So it has to take a class object that's it. So is there any way to do the this?


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @ManojReddy,

    We also don't like this approach for get requests but the library we used (NSWAG) generates service proxy methods like this. As An alternative, you can write a wrapper method for specific cases to convert an object to such query string parameters. Another alternative is converting your method to POST instead of GET.

    In the future, we are planning to handle this in a better way, maybe we will write our own typescript generator but it will not be in a short time.

    Thanks.

  • User Avatar
    0
    manojreddy created

    Hi,

    Thanks for reply.

    Do we have library like nswag which generates proxy file as per my requirement?.

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @ManojReddy,

    We couldn't find such a library. Even if you can find, you will need to change existing codes of AspNet Zero (Angular part) if it's output will be differnet than Nswag's.