0

I have this example of ASP.NET Web Api controller:

    public Mea Get(Int32 id)
    {
        try
        {
            return Meas.GetById(id) ?? new Mea(-1, 0D, 0D);
        }
        catch (Exception)
        {
            return new Mea(-1, 0D, 0D);
        }
    }

I want to be able to return a different response code if the GetById returns null or in the catch exception. All the examples that I see use HttpResponseMessage where you can add the response code. My question is how to change the response code when not using HttpResponseMessage, like in the above example?

2
  • I don't think it can be done. Why don't you like HttpResponseMessage? Commented Oct 30, 2013 at 22:26
  • @sunil It is just about coding convenience, that when you look at the method declaration you see what it returns and not just a generic HttpResponseMessage... Commented Oct 30, 2013 at 23:42

2 Answers 2

1

Well if you want to have different response code to be return from your function then you will have to wrap it in in a HttpResponseMessage.

Your function will always return an status code "200". Even if you return a bare object the framework pipeline will convert it to a HttpResponseMessage with default status code HttpStatusCode.OK, so better you do it by yourself.

What I would suggest is do not return bare objects/values from your API function. Always wrap the values in a HttpResponseMessage and then return a response message from your function.Basically by wrapping it in the HttpResponseMessage you can have proper status code in client side. Returning HttpResponseMessage is always the best practice.

So please change your function like this

public HttpResponseMessage Get(Int32 id)
{
    try
    {
     // on successful execution
      return this.Request.CreateResponse(HttpStatusCode.OK, Meas.GetById(id) ?? new Mea(-1, 0D, 0D));
    }
    catch (Exception)
    {
      return this.Request.CreateResponse(HttpStatusCode.InternalServerError, Mea(-1, 0D, 0D));
    }
}

in the case of an error you can also throw an HttpResponseException

Sign up to request clarification or add additional context in comments.

4 Comments

Why Returning HttpResponseMessage is always the best practice? Whats wrong with returning the object?
@user1615362: even if you return an object it will be wrapped in an HttpResponseMessage automatically by the framework. by returning as HttpResponseMessage you can have more control over the response from your api function also you can customize the response easily if a need arise in the future
Still, from the type safety perspective, it is better to return an "object".
Depending on the situation, when changing the HttpStatucCode, you could also want to return different type of response objects, including a 'cause of error' property for instance. If you want to handle the error case in the controller, it seems to be the way to go. Also read exception-handling from Microsoft.
0

You can throw a System.Web.Http.HttpResponseException, specifying an HTTP status code and/or optionally a whole HttpResponseMessage to be returned to the client.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.