如何在方法中传递对象以在 ASP.NET 样板中调用服务 API?

How to pass object in method to call service API in ASP.NET Boilerplate?

我在AppService文件中定义了一个方法,方法的签名是public PagedResultDto<FetchData> GetSearchData(FetchDataInput searchInput)。我从 Angular 代码调用此方法,但 service-proxoes.ts 文件生成了方法,我需要在其中一一传递所有参数。

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";
        }
    }

}

服务-Proxies.ts文件:

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

所以我必须通过以下方式调用getSearchData方法。

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

所以我必须传递所有的参数,但是如果假设有100个参数,就会很容易出错,而且编程风格也不好。所以它必须使用一个 class 对象。那么有什么办法可以做到这一点吗?

您可以像这样创建一个对象来存储这些参数:

public class FetchDataInput: PagedAndSortedInputDto, IShouldNormalize
{
    public SearchModel searchModel{get; set;}

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

}

public class SearchModel
{
        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; }
}

...在 getSearchData 中,尝试序列化模型并将其传递给您的 api:

_$SearchModelForm= _modalManager.getModal().find('form[name=SearchForm]');
var searchModel = _$SearchModelForm.serializeFormToObject();
this._dataService.getSearchData(searchModel);