5

I tried to use this:

return Request.CreateResponse(HttpStatusCode.InternalServerError, "My message");

also I tried this one:

return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, "My message");

But I see 500 error on my browser though any message like "My message" are displayed.

4
  • You are actually returning 500 error: HttpStatusCode.InternalServerError IS the 500 error. Commented Mar 8, 2013 at 18:50
  • yes, I know, but also we can return BadRequest status code and other status codes, but I still don't see the way how to send with status code my custom message, though method allows me to put my method. Commented Mar 8, 2013 at 18:53
  • Where are you returning this from? Action method in a controller? Commented Mar 8, 2013 at 18:54
  • from Action in mvc controller Commented Mar 8, 2013 at 18:55

3 Answers 3

11

To return a specific response code with a message from ASP.NET MVC controller use:

return new HttpStatusCodeResult(errorCode, "Message");

Make sure the method in the controller is type ActionResult, not ViewResult.

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

3 Comments

this is just an exception, I want to return error with status code, it is another thing.
I'm sorry, I was under the assumption this would do what you needed. I edited the answer.
Bear in mind that this strategy won't work if you internationalise your code. For example 'return new HttpStatusCodeResult(errorCode, "未能删除用户。");' would not return what you'd expect, you'd only see '???????????'. The message text gets returned in an HTTP header, and only ASCII characters are supported (not all Unicode characters anyway).
1

I was able to put a custom message in the error response body with the following code:

Response.TrySkipIisCustomErrors = true;
Response.Clear();
Response.Write( $"Custom Error Message." );
return new HttpStatusCodeResult( HttpStatusCode.InternalServerError );

Comments

0

I have an ErrorController that help me throwing the errors and showing a nice error page, the use Response.StatusCode to return a different StatusCode

namespace Site.Controllers {

    [OutputCache(Location = OutputCacheLocation.None)]
    public class ErrorController : ApplicationController {

        public ActionResult Index() {
            ViewBag.Title = "Error";
            ViewBag.Description = "blah blah";
            return View("Error");
        }

        public ActionResult HttpError404(string ErrorDescription) {
            Response.StatusCode = 404;
            ViewBag.Title = "Page not found (404)";
            ViewBag.Description = "blah blah";
            return View("Error");
        }


        ...

    }
}

Edit For returning message-only to ajax results I use something like this in an actionFilter

        Response.StatusCode = 200;

        //Needed for IIS7.0
        Response.TrySkipIisCustomErrors = true;

        return new ContentResult {
            Content = "ERROR: " + Your_message,
            ContentEncoding = System.Text.Encoding.UTF8
        };

2 Comments

maybe I just use bad approach but I want to receive this message in client side with javascript.
ViewBag will not help me with it

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.