简体   繁体   中英

Error with IHttpActionResult in web api actions

I am facing method not found error in web server, but locally in visual studio it works:

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

Then in browser getting below error:

    <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> 

I have implemented IHttpActionResult as below:

    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);
        }
    } 

The actual method in my project is post but here I am trying to fix with get first then I believe post will also work.

Here to mention the below method work perfectly, so I think something I am missing with IHttpActionResult:

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

Do you have any one faced and solved this problem yet. Please help me, thanks in advance.

Using the CreateResponse extension on the request would allow any configuration from the request to be copied over to the response which would probably be missing when you create the response manually like in your example.

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);
    }
}

Also the controller action is not defined correctly as it is defined as async Task<IHttpActionResult> when the action is not doing anything async.

Refactor to follow correct syntax if not actually asynchronous.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM