这是在nswag swagger客户端生成的帮助下从C#控制器自动生成的Angular服务文件。下面的代码提供了所有服务。在组件中,我试图调用getCompanyInfoOption()api。但是我没有收到我从控制器发送来的响应数据。
/* tslint:disable */
/* eslint-disable */
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v13.6.0.0 (NJsonSchema v10.1.18.0 (Newtonsoft.Json v12.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------
// ReSharper disable InconsistentNaming
import { mergeMap as _observableMergeMap, catchError as _observableCatch } from 'rxjs/operators';
import { Observable, throwError as _observableThrow, of as _observableOf } from 'rxjs';
import { Injectable, Inject, Optional, InjectionToken } from '@angular/core';
import { HttpClient, HttpHeaders, HttpResponse, HttpResponseBase } from '@angular/common/http';
export const API_BASE_URL = new InjectionToken<string>('API_BASE_URL');
@Injectable({
providedIn:'root'
})
export class CompanyInfoService {
private http: HttpClient;
private baseUrl: string;
protected jsonParseReviver: ((key: string, value: any) => any) | undefined = undefined;
constructor(@Inject(HttpClient) http: HttpClient, @Optional() @Inject(API_BASE_URL) baseUrl?: string) {
this.http = http;
this.baseUrl = baseUrl ? baseUrl : "https://localhost:44311";
}
getCompanyInfoOption(): Observable<FileResponse | null> {
let url_ = this.baseUrl + "/api/company-info";
url_ = url_.replace(/[?&]$/, "");
let options_ : any = {
observe: "response",
responseType: "blob",
headers: new HttpHeaders({
"Accept": "application/octet-stream"
})
};
return this.http.request("get", url_, options_).pipe(_observableMergeMap((response_ : any) => {
return this.processGetCompanyInfoOption(response_);
})).pipe(_observableCatch((response_: any) => {
if (response_ instanceof HttpResponseBase) {
try {
return this.processGetCompanyInfoOption(<any>response_);
} catch (e) {
return <Observable<FileResponse | null>><any>_observableThrow(e);
}
} else
return <Observable<FileResponse | null>><any>_observableThrow(response_);
}));
}
protected processGetCompanyInfoOption(response: HttpResponseBase): Observable<FileResponse | null> {
const status = response.status;
const responseBlob =
response instanceof HttpResponse ? response.body :
(<any>response).error instanceof Blob ? (<any>response).error : undefined;
let _headers: any = {}; if (response.headers) { for (let key of response.headers.keys()) { _headers[key] = response.headers.get(key); }}
if (status === 200 || status === 206) {
const contentDisposition = response.headers ? response.headers.get("content-disposition") : undefined;
const fileNameMatch = contentDisposition ? /filename="?([^"]*?)"?(;|$)/g.exec(contentDisposition) : undefined;
const fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[1] : undefined;
return _observableOf({ fileName: fileName, data: <any>responseBlob, status: status, headers: _headers });
} else if (status !== 200 && status !== 204) {
return blobToText(responseBlob).pipe(_observableMergeMap(_responseText => {
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
}));
}
return _observableOf<FileResponse | null>(<any>null);
}
如何在组件中调用此服务?我正在尝试使用组件的ngOnInit中的subscription方法进行调用
ngOnInit(): void {
this.service.getCompanyInfoOption().subscribe(
value => {
console.log(value);
},
error => console.error(JSON.stringify(error)),
() => console.log('done')
);
}
我在调用getCompanyInfoOption api后得到此输出
我的c#控制器
ok中发送的compantInfoOption的响应类型如下
答案 0 :(得分:0)
看来你是。因为您的响应类型为FileResponse
,并且在console.log中,您可能会看到此对象。在数据中存储的另一件事是blob
,如果需要文本解释(如果它只是使用blobToText
函数之类的smth,该函数位于生成的文件的末尾)。
如果我错了,请在问题中添加期望的结果。
答案 1 :(得分:0)
我猜生成的代码有问题,因为您的控制器返回类型不正确,请尝试使用ActionResult,然后生成的api将返回正确的结果,而不是期望文件响应。
以您的情况
public async Task<ActionResult<CompanyInfoOptionAc>> GetCompanyInfo(){
//etc
}