I\'ve got a couple of services which already receive a json string (not an object) that must be returned to the client. Currently, I\'m creating the HttpResponseMessag
Set your Web Api to return JSON Format:
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
// Force to ignore Request Content Type Header and reply only JSON
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
var corsAttr = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(corsAttr);
}
and then return response like this:
[HttpGet]
[Route("{taskId}/list")]
public IHttpActionResult GetTaskDocuments(string taskId)
{
var docs = repository.getTaskDocuments(taskId);
if (docs != null)
{
return Ok(docs);
}
else
{
return Ok(new ResponseStatus() { Status = Constants.RESPONSE_FAIL, Message = repository.LastErrorMsg });
}
}
Where ResponseStatus is next class:
public class ResponseStatus
{
public string Status { get; set; }
public string Message { get; set; }
}