Take the 2-minute tour ×

This question already has an answer here:

I need to throw an exception from my asp.net mvc controller to return to the client.

I can do:

throw new HttpResponseException(HttpStatusCode.NotAcceptable); to return a status code.

or I can do:

throw new HttpRequestException("You must fill the name!"); to return a message.

My question: is it possible to combine both solutions to provide a status code AND a message?

Somethink like:

throw new HttpRequestException(HttpStatusCode.NotAcceptable, "Fill the name!");



Please note that this is an asp.net MVC website but using the Breeze framework. I need to throw an exception when my validation failed (so before saving my changes server side).

public class TPBContextProvider : EFContextProvider<BreezeContext>

    protected override bool BeforeSaveEntity(EntityInfo entityInfo)  
        throw new HttpResponseException(HttpStatusCode.Conflict);
share|improve this question

marked as duplicate by CodeCaster, Rowland Shaw, George Stocker Jul 17 '13 at 16:46


I don't think your DAL should throw HTTP errors. Consider using your own exception class, like ContextException, which you may give a specific error code which you catch in your action method, where you in turn can translate that code into an HTTP status code and a message. –  CodeCaster Jul 15 '13 at 13:53

2 Answers 2

Have you tried returning an HttpStatusCodeResult instead of throwing up:

public ActionResult Index()
    return new HttpStatusCodeResult(HttpStatusCode.NotAcceptable, "Fill the name!")
share|improve this answer
To be more precise, this is effectively an asp.net mvc website but using the breeze framework. I need to throw an error but not from an ActionResult, I updated my question. Thanks. –  Bronzato Jul 15 '13 at 13:42
@bronzato : stackoverflow.com/a/17653933/2015869 –  Imad Alazani Jul 16 '13 at 5:23

I think that this will work

  var responseMsg = new HttpResponseMessage(HttpStatusCode.Conflict);
  responseMsg.Content = new StringContent("Custom error message");
  throw new HttpResponseException(responseMsg);
share|improve this answer
