首页 > 解决方案 > 使用来自 Web API 和 Swagger 的状态的正确返回类型

问题描述

这个问题和这个问题一样。我想从具有相关 html 状态的 IActionResult 获取对象,但 Swagger 向我显示“OK”消息而不是相关状态。

我搜索了很多小时以发现配置中的错误,但没有找到任何解决问题的方法。我将向您展示代码:

这是 IActionResult:

[HttpPut("updateuserpricingplan")]
[ProducesResponseType(200)]
[ProducesResponseType(400, Type = typeof(ServiceValidationResult))]
public async Task<IActionResult> UpdateSelection([FromBody]UpdatePricingPlanModel model)
{
    var user = await _userService.GetSignedIdUser(this.HttpContext);
    var result = await _userService.UpdateUserPricingPlan(user, model.PricingPlanId, _settingService, model.Code);

    if (result.Succeeded)
        return Ok();

    return new ObjectResult(result) { StatusCode = 400 };
}

知道我可以从 Google Chrome 的“网络/响应”选项卡中看到结果和状态代码,这很有趣。

这是我写订阅的方式

this.pricingPlanService.updateSelection(model)
      .subscribe(res => {
        console.log(res.result);
      }, err => { // <== I got "OK" result as string and nothing else
        console.log(err.status);
        console.log(err.response);
      }, () => {
        this.sessionService.updatePricingPlan(selectedPricingPlan.id);
      });

现在这里是我使用的版本:

如果有帮助,这里是 NSwagStudio 为 updateSelection 生成的代码。请注意,if (response_ instanceof HttpResponseBase)接缝始终是假的。

updateSelection(model: UpdatePricingPlanModel): Observable<SwaggerResponse<void>> {
        let url_ = this.baseUrl + "/api/PricingPlan/updateuserpricingplan";
        url_ = url_.replace(/[?&]$/, "");

        const content_ = JSON.stringify(model);

        let options_ : any = {
            body: content_,
            observe: "response",
            responseType: "blob",
            headers: new HttpHeaders({
                "Content-Type": "application/json", 
            })
        };

        return this.http.request("put", url_, options_).pipe(_observableMergeMap((response_ : any) => {
            return this.processUpdateSelection(response_);
        })).pipe(_observableCatch((response_: any) => {
            if (response_ instanceof HttpResponseBase) {
                try {
                    return this.processUpdateSelection(<any>response_);
                } catch (e) {
                    return <Observable<SwaggerResponse<void>>><any>_observableThrow(e);
                }
            } else
                return <Observable<SwaggerResponse<void>>><any>_observableThrow(response_);
        }));
    }

    protected processUpdateSelection(response: HttpResponseBase): Observable<SwaggerResponse<void>> {
        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) {
            return blobToText(responseBlob).pipe(_observableMergeMap(_responseText => {
            return _observableOf<SwaggerResponse<void>>(new SwaggerResponse(status, _headers, <any>null));
            }));
        } else if (status === 400) {
            return blobToText(responseBlob).pipe(_observableMergeMap(_responseText => {
            let result400: any = null;
            let resultData400 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
            result400 = resultData400 ? ServiceValidationResult.fromJS(resultData400) : <any>null;
            return throwException("A server error occurred.", status, _responseText, _headers, result400);
            }));
        } else if (status !== 200 && status !== 204) {
            return blobToText(responseBlob).pipe(_observableMergeMap(_responseText => {
            return throwException("An unexpected server error occurred.", status, _responseText, _headers);
            }));
        }
        return _observableOf<SwaggerResponse<void>>(new SwaggerResponse(status, _headers, <any>null));
    }

您是否看到任何错误或配置错误?

感谢您的任何帮助,

标签: c#swaggerswagger-uiasp.net-core-2.2

解决方案


推荐阅读