For returning from a Web API 2 controller, I can return content with the response if the response is OK (status 200) like this:
public IHttpActionResult
A more detailed example with support of HTTP code not defined in C# HttpStatusCode
.
public class MyController : ApiController
{
public IHttpActionResult Get()
{
HttpStatusCode codeNotDefined = (HttpStatusCode)429;
return Content(codeNotDefined, "message to be sent in response body");
}
}
Content
is a virtual method defined in abstract class ApiController
, the base of the controller. See the declaration as below:
protected internal virtual NegotiatedContentResult<T> Content<T>(HttpStatusCode statusCode, T value);
I would recommend reading this post. There are tons of ways to use existing HttpResponse as suggested, but if you want to take advantage of Web Api 2, then look at using some of the built-in IHttpActionResult options such as
return Ok()
or
return NotFound()
Choose the right return type for Web Api Controllers
Above things are really helpful.
While creating web services, If you will take case of services consumer will greatly appreciated. I tried to maintain uniformity of the output. Also you can give remark or actual error message. The web service consumer can only check IsSuccess true or not else will sure there is problem, and act as per situation.
public class Response
{
/// <summary>
/// Gets or sets a value indicating whether this instance is success.
/// </summary>
/// <value>
/// <c>true</c> if this instance is success; otherwise, <c>false</c>.
/// </value>
public bool IsSuccess { get; set; } = false;
/// <summary>
/// Actual response if succeed
/// </summary>
/// <value>
/// Actual response if succeed
/// </value>
public object Data { get; set; } = null;
/// <summary>
/// Remark if anythig to convey
/// </summary>
/// <value>
/// Remark if anythig to convey
/// </value>
public string Remark { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the error message.
/// </summary>
/// <value>
/// The error message.
/// </value>
public object ErrorMessage { get; set; } = null;
}
[HttpGet]
public IHttpActionResult Employees()
{
Response _res = new Response();
try
{
DalTest objDal = new DalTest();
_res.Data = objDal.GetTestData();
_res.IsSuccess = true;
return Ok<Response>(_res);
}
catch (Exception ex)
{
_res.IsSuccess = false;
_res.ErrorMessage = ex;
return ResponseMessage(Request.CreateResponse(HttpStatusCode.InternalServerError, _res ));
}
}
You are welcome to give suggestion if any :)
You can use this:
return Content(HttpStatusCode.BadRequest, "Any object");
Sorry for the late answer why don't you simple use
return BadRequest("your message");
I use it for all my IHttpActionResult
errors its working well
here is the documentation : https://msdn.microsoft.com/en-us/library/system.web.http.apicontroller.badrequest(v=vs.118).aspx
I ended up going with the following solution:
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);
}
}
... which can be used like this:
public IHttpActionResult Get()
{
return new HttpActionResult(HttpStatusCode.InternalServerError, "error message"); // can use any HTTP status code
}
I'm open to suggestions for improvement. :)