How do I return NotFound() IHttpActionResult with an error message or exception?
Asked Answered
K

13

127

I am returning a NotFound IHttpActionResult, when something is not found in my WebApi GET action. Along with this response, I want to send a custom message and/or the exception message (if any). The current ApiController's NotFound() method does not provide an overload to pass a message.

Is there any way of doing this? or I will have to write my own custom IHttpActionResult?

Koenraad answered 22/11, 2013 at 7:43 Comment(2)
Do you want to return the same message for all Not Found results?Assets
@NikolaiSamteladze No, it could be a different message depending on the situation.Koenraad
C
286

Here's a one-liner for returning a IHttpActionResult NotFound with a simple message:

return Content(HttpStatusCode.NotFound, "Foo does not exist.");
Cassimere answered 19/6, 2015 at 16:14 Comment(6)
People should vote up this answer. It's nice and easy!Jesse
Be aware that this solution do not set the HTTP header status to "404 Not Found".Lusatian
@KasperHalvasJensen The http status code from the server is 404, do you need something more?Cassimere
@AnthonyF You are right. I was using the Controller.Content(...). Shoud have used the ApiController.Content(...) - My bad.Lusatian
Thanks mate, this was exactly what I was looking forUniformize
This answer should have more votes, the object passes is still serialized. Works like a charm.Terrilynterrine
B
90

You'd need to write your own action result if you want to customize the response message shape.

We wanted to provide the most common response message shapes out of the box for things like simple empty 404s, but we also wanted to keep these results as simple as possible; one of the main advantages of using action results is that it makes your action method much easier to unit test. The more properties we put on action results, the more things your unit test needs to consider to make sure the action method is doing what you'd expect.

I often want the ability to provide a custom message as well, so feel free to log a bug for us to consider supporting that action result in a future release: https://aspnetwebstack.codeplex.com/workitem/list/advanced

One nice thing about action results, though, is that you can always write your own fairly easily if you want to do something slightly different. Here's how you might do it in your case (assuming you want the error message in text/plain; if you want JSON, you'd do something slightly different with the content):

public class NotFoundTextPlainActionResult : IHttpActionResult
{
    public NotFoundTextPlainActionResult(string message, HttpRequestMessage request)
    {
        if (message == null)
        {
            throw new ArgumentNullException("message");
        }

        if (request == null)
        {
            throw new ArgumentNullException("request");
        }

        Message = message;
        Request = request;
    }

    public string Message { get; private set; }

    public HttpRequestMessage Request { get; private set; }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        return Task.FromResult(Execute());
    }

    public HttpResponseMessage Execute()
    {
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NotFound);
        response.Content = new StringContent(Message); // Put the message in the response body (text/plain content).
        response.RequestMessage = Request;
        return response;
    }
}

public static class ApiControllerExtensions
{
    public static NotFoundTextPlainActionResult NotFound(this ApiController controller, string message)
    {
        return new NotFoundTextPlainActionResult(message, controller.Request);
    }
}

Then, in your action method, you can just do something like this:

public class TestController : ApiController
{
    public IHttpActionResult Get()
    {
        return this.NotFound("These are not the droids you're looking for.");
    }
}

If you used a custom controller base class (instead of directly inheriting from ApiController), you could also eliminate the "this." part (which is unfortunately required when calling an extension method):

public class CustomApiController : ApiController
{
    protected NotFoundTextPlainActionResult NotFound(string message)
    {
        return new NotFoundTextPlainActionResult(message, Request);
    }
}

public class TestController : CustomApiController
{
    public IHttpActionResult Get()
    {
        return NotFound("These are not the droids you're looking for.");
    }
}
Bud answered 22/11, 2013 at 18:29 Comment(4)
I wrote exactly similar implementation of 'IHttpActionResult', but not specific for 'NotFound' result. This will probably work for All 'HttpStatusCodes'. My CustomActionResult code looks something like this And my controler's 'Get()' action looks like this: 'public IHttpActionResult Get() { return CustomNotFoundResult("Meessage to Return."); }' Also, I logged a bug on CodePlex for considering this in the future release.Koenraad
I use ODataControllers and I had to use this.NotFound("blah");Gurney
Very nice post, but I'd just like to recommend against the inheritance tip. My team decided to do exactly that a long time ago, and it bloated the classes a lot by doing it. I just recently refactored all of it into extension methods and moved away from the inheritance chain. I'd seriously recommend people to carefully consider when they should use inheritance like this. Usually, composition is a lot better, because it is a lot more decoupled.Ocker
This functionality should have been out-of-the-box. Including an optional "ResponseBody" parameter should not affect unit-tests.Belayneh
Y
29

You could use ResponseMessageResult if you like:

var myCustomMessage = "your custom message which would be sent as a content-negotiated response"; 
return ResponseMessage(
    Request.CreateResponse(
        HttpStatusCode.NotFound, 
        myCustomMessage
    )
);

yeah, if you need much shorter versions, then I guess you need to implement your custom action result.

Yabber answered 22/11, 2013 at 14:35 Comment(2)
I went with this method as it seemed neat. I just defined the custom message elsewhere and indented return code.Syphon
I like this better than Content because it actually returns a object I can parse with a Message property just like the standard BadRequest method.Cropdusting
K
8

You may use ReasonPhrase property of HttpResponseMessage class

catch (Exception exception)
{
  throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)
  {
    ReasonPhrase = exception.Message
  });
}
Katey answered 22/11, 2013 at 7:49 Comment(6)
Thanks. Well.. this should work, but then I will have to build the HttpResponseException on my own in every action. To keep the code less, I was thinking if I could use any WebApi 2 features (just like the ready-made NotFount(), Ok() methods) and pass the ReasonPhrase message to it.Koenraad
You may create your own extension method NotFound(Exception exception), which will throw correct HttpResponseExceptionKatey
@DmytroRudenko: action results were introduced to improve testability. By throwing HttpResponseException here you would be compromising that. Also here we do not have any exception, but the OP is looking for sending back a message.Yabber
Ok, if you don't want use NUint for testing, you can write your own implementation of NotFoundResult and rewrite its ExecuteAsync for returning your message data. And return instance of this class as a result of your action invocation.Katey
Note that now you can pass the status code directly, e.g. HttpResponseException(HttpStatusCode.NotFound)Im
Will not work if exception.Message contains e.g. a line break.Luxury
H
5

one line code in asp.net core:

Return StatusCode(404, "Not a valid request.");
Hillell answered 19/1, 2022 at 20:47 Comment(0)
D
4

You can create a custom negotiated content result as d3m3t3er suggested. However I would inherit from. Also, if you need it only for returning NotFound, you don't need to initialize the http status from constructor.

public class NotFoundNegotiatedContentResult<T> : NegotiatedContentResult<T>
{
    public NotFoundNegotiatedContentResult(T content, ApiController controller)
        : base(HttpStatusCode.NotFound, content, controller)
    {
    }

    public override Task<HttpResponseMessage> ExecuteAsync(
        CancellationToken cancellationToken)
    {
        return base.ExecuteAsync(cancellationToken).ContinueWith(
            task => task.Result, cancellationToken);
    }
}
Discourteous answered 10/7, 2014 at 12:48 Comment(0)
K
2

I solved it by simply deriving from OkNegotiatedContentResult and overriding the HTTP code in the resulting response message. This class allows you to return the content body with any HTTP response code.

public class CustomNegotiatedContentResult<T> : OkNegotiatedContentResult<T>
{
    public HttpStatusCode HttpStatusCode;

    public CustomNegotiatedContentResult(
        HttpStatusCode httpStatusCode, T content, ApiController controller)
        : base(content, controller)
    {
        HttpStatusCode = httpStatusCode;
    }

    public override Task<HttpResponseMessage> ExecuteAsync(
        CancellationToken cancellationToken)
    {
        return base.ExecuteAsync(cancellationToken).ContinueWith(
            task => { 
                // override OK HTTP status code with our own
                task.Result.StatusCode = HttpStatusCode;
                return task.Result;
            },
            cancellationToken);
    }
}
Kolnos answered 6/6, 2014 at 13:55 Comment(0)
C
2

I was needing to create an IHttpActionResult instance in the body of an IExceptionHandler class, in order to set the ExceptionHandlerContext.Result property. However I also wanted to set a custom ReasonPhrase.

I found that a ResponseMessageResult could wrap a HttpResponseMessage (which allows ReasonPhrase to be set easily).

For Example:

public class MyExceptionHandler : ExceptionHandler
{
    public override void Handle(ExceptionHandlerContext context)
    {
        var ex = context.Exception as IRecordNotFoundException;
        if (ex != null)
        {
            context.Result = new ResponseMessageResult(new HttpResponseMessage(HttpStatusCode.NotFound) { ReasonPhrase = $"{ex.EntityName} not found" });
        }
    }
}
Croce answered 4/5, 2016 at 7:23 Comment(0)
I
2

Another nice possibility is to use a different built-in result type: NotFoundObjectResult(message).

Inflight answered 18/10, 2022 at 8:19 Comment(0)
K
1

If you inherit from the base NegotitatedContentResult<T>, as mentioned, and you don't need to transform your content (e.g. you just want to return a string), then you don't need to override the ExecuteAsync method.

All you need to do is provide an appropriate type definition and a constructor that tells the base which HTTP Status Code to return. Everything else just works.

Here are examples for both NotFound and InternalServerError:

public class NotFoundNegotiatedContentResult : NegotiatedContentResult<string>
{
    public NotFoundNegotiatedContentResult(string content, ApiController controller)
        : base(HttpStatusCode.NotFound, content, controller) { }
}

public class InternalServerErrorNegotiatedContentResult : NegotiatedContentResult<string>
{
    public InternalServerErrorNegotiatedContentResult(string content, ApiController controller)
        : base(HttpStatusCode.InternalServerError, content, controller) { }
}

And then you can create corresponding extension methods for ApiController (or do it in a base class if you have one):

public static NotFoundNegotiatedContentResult NotFound(this ApiController controller, string message)
{
    return new NotFoundNegotiatedContentResult(message, controller);
}

public static InternalServerErrorNegotiatedContentResult InternalServerError(this ApiController controller, string message)
{
    return new InternalServerErrorNegotiatedContentResult(message, controller);
}

And then they work just like the built-in methods. You can either call the existing NotFound() or you can call your new custom NotFound(myErrorMessage).

And of course, you can get rid of the "hard-coded" string types in the custom type definitions and leave it generic if you want, but then you may have to worry about the ExecuteAsync stuff, depending on what your <T> actually is.

You can look over the source code for NegotiatedContentResult<T> to see all it does. There isn't much to it.

Kriemhild answered 22/10, 2015 at 19:5 Comment(0)
C
1

Iknow PO asked with a message text, but another option to just return a 404 is making the method return a IHttpActionResult and use the StatusCode function

    public async Task<IHttpActionResult> Get([FromUri]string id)
    {
       var item = await _service.GetItem(id);
       if(item == null)
       {
           StatusCode(HttpStatusCode.NotFound);
       }
       return Ok(item);
    }
Communitarian answered 6/9, 2019 at 16:24 Comment(0)
V
1

Answers here are missing a little developer story problem. The ApiController class is still exposing a NotFound() method that developers may use. This would cause some 404 response to contain a uncontrolled result body.

I present here a few parts of code "better ApiController NotFound method" that will provide a less error-prone method that does not require developers to know "the better way of sending a 404".

  • create a class inheriting from ApiController called ApiController
    • I use this technique to prevent developers from using the original class
  • override its NotFound method to let devs use the first available api
  • if you want to discourage this, mark this as [Obsolete("Use overload instead")]
  • add an extra protected NotFoundResult NotFound(string message) that you want to encourage
  • problem: the result does not support responding with a body. solution: inherit and use NegotiatedContentResult. see attached better NotFoundResult class.
Viperine answered 6/11, 2019 at 11:2 Comment(0)
C
0

Needed to return the error message for 404 Not Found and I am using Dot Net 6.0.

This is the code

Problem(statusCode: 404, detail: "Put your detailed error message here");

Where Problem is a method present in ControllerBase class.

Canicula answered 16/12, 2022 at 6:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.