使用Web API和Swagger中的状态更正返回类型

时间:2019-04-16 03:16:32

标签: c# swagger swagger-ui asp.net-core-2.2

问题与this one相同。我想从IActionResult获取具有相关html状态的对象,但是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:12.1.0.0
  • 角度:7.0.4
  • ASP.net Core 2.2.4

如果有帮助,请在此处输入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));
    }

您看到任何错误或配置错误吗?

谢谢您的帮助,

0 个答案:

没有答案