locked
Web Api Status Code RRS feed

  • Question

  • User-1177023326 posted

    Good Day All,

    Please can any help to resolve this. I have an IHTTPACTIONRESULT which is working but i want to customize a message saying Record Not Found when it returns null.

    This is my code 

    public IHttpActionResult Get(string Org)
    {
    var organisations = repository.Get(Org);

    if (organisations == null)

    return NotFound();
    return Ok(organisations);
    }

    I tried this

    public IHttpActionResult Get(string Org)

    var organisations = repository.Get(Org);


    {if(organisations == null)


     return new HttpActionResult(HttpStatusCode.NotFound, "Record Not Found");


    return new HttpActionResult(HttpStatusCode.Ok, "This is your record");

    }

    The code continue to return Record Not Found even when the record is Not empty or  null. It returns 200 for both null and when is not null. How can i resolve this issue.

    Thank You

    Friday, August 12, 2016 1:45 PM

Answers

  • User36583972 posted

    Hi moruff,

    What i actually  want is that i want it to return status code 404 Not found with my customize error when search record is not available, but return status code 200 ok when search record is available.

    This is a simple logic. You should check the type of [var organisations = repository.Get(Org)]. You can use Object.length or Object.Count to check search record is not available. organisations == null is a variable is not initialized.

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 15, 2016 8:41 AM

All replies

  • User36583972 posted

    Hi moruff,

    You can try the following code.

    HttpActionResult:

    public class HttpActionResult : IHttpActionResult
        {
            private readonly string _message;
            private readonly HttpStatusCode _statusCode;
    
            public HttpActionResult(HttpStatusCode statusCode, string message)
            {
                _statusCode = statusCode;
                _message = message;
            }
    
            public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
            {
                HttpResponseMessage response = new HttpResponseMessage(_statusCode)
                {
                    Content = new StringContent(_message)
                };
                return Task.FromResult(response);
            }
        }
    

    Controller:

       public IHttpActionResult GetTeam(int id)
            {
                //Team team = db.Teams.Find(id);
                Team team = null;
                if (team == null)
                {
                    return new HttpActionResult(HttpStatusCode.InternalServerError, " Record Not Found."); // can use any HTTP status code
                    //return NotFound();
                }
                return Ok(team);
            }
    

    Best Regards,

    Yohann Lu

    Monday, August 15, 2016 3:06 AM
  • User-1177023326 posted

    Hi Yohann Lu,

    Thank You for response. But the point is this  it return Not Found even when record is available. 

    public IHttpActionResult GetIndividual(string Org)
    {
    //public IHttpActionResult GetIndividual(string Org)
    var organisations = repository.Get(Org);
    //organisations = null;
    if (organisations == null)
    {
    return new HttpActionResult(HttpStatusCode.NotFound, " Record Not Found."); // can use any HTTP status code
    //return NotFound();
    }
    return Ok(organisations);

    }

    and when i added organization = null it was returning Empty record all through. But without it it will return status code 200 ok and and the informaton when my search record is available. What i actually  want is that i want it to return status code 404 Not found with my customize error when search record is not available, but return status code 200 ok when search record is available.

    Thank YOU

    Monday, August 15, 2016 8:08 AM
  • User36583972 posted

    Hi moruff,

    What i actually  want is that i want it to return status code 404 Not found with my customize error when search record is not available, but return status code 200 ok when search record is available.

    This is a simple logic. You should check the type of [var organisations = repository.Get(Org)]. You can use Object.length or Object.Count to check search record is not available. organisations == null is a variable is not initialized.

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 15, 2016 8:41 AM