Home > Net >  "Non-invocable member ' ' cannot be used like a method" when using BadRequest(),
"Non-invocable member ' ' cannot be used like a method" when using BadRequest(),

Time:11-29

I have a function public Task<IActionResult> Post(IFormFile file) that gets called when an endpoint for my API is used. In this function I do some business logic and eventually want to return something back to the client. In this case I am trying to use the BadRequest(), Ok() and InternalServerErrorResult() functions from Microsoft.AspNetCore.Http.HttpResults but am running into a compiler error saying "Non-invocable member 'BadRequest' cannot be used like a method" and similiarly for Ok() and InternalServerErrorResult(). I do not understand why this is happening. The used functions are clearly exactly that, not properties or something else. Why am I getting this error?

public Task<IActionResult> Post(IFormFile file)
{
    if (file.ContentType != "audio/wave")
        return BadRequest("You have provided a file with an unsupported file extension.\nThe only supported file extensions are: [wav]");

    try
    {

        var pathToSoundFile = Path.Combine(ml.InfileLocation, file.FileName);
        Predictor predictor = Predictor.Instance;
        (String label, float score) = predictor.Predict(pathToSoundFile);
        return Ok($"Result: {label}\nScore: {score}");
     }
     catch (Exception ex)
     {
         return InternalServerErrorResult(ex);
     }
}

I have browsed the documentation for the mentioned ApiController response methods to confirm that indeed they are functions and are to be used in the way I have. I do not know what else I can try.

CodePudding user response:

Those methods are part of the ApiController class, so your controller needs to inherit from that class before you can use them. I suspect you're missing that.

For example:

public class MyController : ApiController {
    ...
}

But InternalServerErrorResult() is still incorrect. It should be InternalServerError() (no Result).

CodePudding user response:

I could be wrong but maybe it's complaining about the new line (\n). Can you please remove the new lines from your code or add this to your code

HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, strResponse);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");

CodePudding user response:

I don't see InternalServerErrorResult to be a static class and also I don't see an instance of it. Can you try: return new InternalServerErrorResult(ex); ?

  • Related