【问题标题】:Error with IHttpActionResult in web api actionsWeb api 操作中的 IHttpActionResult 错误
【发布时间】:2018-12-17 10:45:44
【问题描述】:

我在 Web 服务器中遇到方法未找到错误,但在本地 Visual Studio 中它可以工作:

    [HttpGet]
    [Route("api/checkhealth")]
    public async Task<IHttpActionResult> CheckHealth()
    {
        var message = "checkhealth method was invoked";
        return new TextResult(message, Request);
    }

然后在浏览器中出现以下错误:

    <Error>
    <Message>An error has occurred.</Message>
    <ExceptionMessage>Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get_Request()'.
    </ExceptionMessage>
    <ExceptionType>System.MissingMethodException</ExceptionType>
    <StackTrace>at BMI.Controllers.APIController.<CheckHealth>d__0.MoveNext() at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine) at BMI.Controllers.APIController.CheckHealth() at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_3.<GetExecutor>b__2(Object instance, Object[] methodParameters) at 
      System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at 
      System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker. 
       <InvokeActionAsyncCore>d__1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult. 
       <ExecuteAsync>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher. 
      <SendAsync>d__15.MoveNext()
      </StackTrace>
     </Error> 

我已经实现了 IHttpActionResult 如下:

    public class TextResult : IHttpActionResult
    {
        string message;
        HttpRequestMessage request;

        public TextResult(string message, HttpRequestMessage request)
        {
            this.message = message;
            this.request = request;
        }
        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            var response = new HttpResponseMessage()
            {
                Content = new StringContent(message),
                RequestMessage = request
            };
            return Task.FromResult(response);
        }
    } 

我项目中的实际方法是 post,但在这里我尝试先使用 get 进行修复,然后我相信 post 也可以。

这里提一下下面的方法工作得很好,所以我认为 IHttpActionResult 缺少一些东西:

    [HttpGet]
    [Route("api/getok")]
    public JsonResult<string> getJson()
    { 
        return Json("OK");
    }

你有没有人遇到过并解决了这个问题。请帮助我,提前谢谢。

【问题讨论】:

    标签: c# asp.net-web-api asp.net-web-api2


    【解决方案1】:

    在请求上使用 CreateResponse 扩展名将允许将请求中的任何配置复制到响应中,当您像示例中那样手动创建响应时,这可能会丢失。

    public class TextResult : IHttpActionResult {
        string message;
        HttpRequestMessage request;
    
        public TextResult(string message, HttpRequestMessage request) {
            this.message = message;
            this.request = request;
        }
        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) {
            var response = request.CreateResponse(HttpStatusCode.OK, message);
            return Task.FromResult(response);
        }
    }
    

    此外,控制器操作未正确定义,因为当操作未执行任何异步操作时,它被定义为 async Task&lt;IHttpActionResult&gt;

    如果实际上不是异步的,则重构以遵循正确的语法。

    [HttpGet]
    [Route("api/checkhealth")]
    public IHttpActionResult CheckHealth() {
        var message = "checkhealth method was invoked";
        return new TextResult(message, Request);
    }
    

    【讨论】:

    • 您好 Nkosi,感谢您的回答,我将尝试使用 CreateResponse 扩展方法。项目中的原始 Post 方法具有异步功能,因此我在这里使用了类似的方法。我会尝试不使用异步,让我们看看。但我想我已经在很多试验中尝试过 CreateResponse。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    相关资源
    最近更新 更多