或者使用动态返回StatusCode(,);这是使用Microsoft.AspNetCore.Mvc 下面是这个.StatusCode 详细说明了一点:/* "this" 来自你的类是 Microsoft.AspNetCore.Mvc.ControllerBase 的子类 */StatusCodeResult scr = this.StatusCode(200);/* 或者 */Object myObject = new Object();ObjectResult 矿石 = this.StatusCode(200, myObject);返回 scr;/* 或返回矿石;*/I'm porting over some code that was previously written in .NET Framework to .NET Core.I had something like this:HttpResponseMessage result = await client.SendAync(request);if (result.StatusCode != HttpStatusCode.OK){ IHttpActionResult response = ResponseMessage(result); return response;}The return value of this function is now IActionResult.How do I take the HttpResponseMessage result object and return an IActionResult from it? 解决方案 You can return using hardset status codes like Ok(); or BadRequest();Or return using a dynamic one usingStatusCode(<Your status code number>,<Optional return object>);This is using Microsoft.AspNetCore.Mvc Below is this.StatusCode spelled out a little more:/* "this" comes from your class being a subclass of Microsoft.AspNetCore.Mvc.ControllerBase */StatusCodeResult scr = this.StatusCode(200);/* OR */Object myObject = new Object();ObjectResult ores = this.StatusCode(200, myObject);return scr; /* or return ores;*/ 这篇关于在 .NET Core 中从 HttpResponseMessage 转换为 IActionResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 20:08