【发布时间】:2020-05-31 06:19:45
【问题描述】:
这是在 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 中的 subscribe 方法调用
ngOnInit(): void {
this.service.getCompanyInfoOption().subscribe(
value => {
console.log(value);
},
error => console.error(JSON.stringify(error)),
() => console.log('done')
);
}
我在调用 getCompanyInfoOption api 后得到这个输出
我的 c# 控制器
compantInfoOption send in ok 的响应类型如下所示
【问题讨论】: