0

I am currently working on Asp.net WebAPI2 Project. I am making a custom method for returning custom HttpStatusCode for error handling.

here's my code

    public IHttpActionResult GetError(int errCode) {
        int ErrCode = errCode * -1;
        ResourceManager ResourceManager = new ResourceManager("PIJ.API.OWIN.Properties.Resources", Assembly.GetExecutingAssembly());
        string ErrorMessage = ResourceManager.GetString("_" + ErrCode.ToString());

        return Content((HttpStatusCode)ErrCode, new HttpError(ErrorMessage));
    }

my current output is like this Output Screenshot

my question is. Is is possible to return only the HttpStatusCode, without the error Message ?

Desired Output

Thank you very much.

3
  • 2
    You should post the code instead of screenshots Commented Jul 26, 2016 at 5:21
  • @Prima, Welcome to Stack Overflow! Please post code as plain text in the body of your question. Adding it as an image makes it very difficult for people to adapt your code to an answer, you can't copy-paste it, and it's hostile to those dependent on screen-readers Commented Jul 26, 2016 at 5:34
  • Thank you for the suggestion.. Sorry this is my first time asking question Commented Jul 26, 2016 at 6:35

1 Answer 1

1

You can return StatusCodeResult with required HttpStatusCode. This can be done using StatusCode method of ApiController

public IHttpActionResult Test()
{
    return StatusCode(HttpStatusCode.BadRequest);
}
Sign up to request clarification or add additional context in comments.

2 Comments

is it possible to return a custom status code ? .. for example I want to return Error 600
yes, you can do this StatusCode((HttpStatusCode)600);

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.