Skip to content

Instantly share code, notes, and snippets.

@birksy89
Created March 8, 2017 11:38
Show Gist options
  • Save birksy89/b8423cc027b7d9d9b0ffc542c3758172 to your computer and use it in GitHub Desktop.
Save birksy89/b8423cc027b7d9d9b0ffc542c3758172 to your computer and use it in GitHub Desktop.
Return JSON - DNN Web API
[AllowAnonymous]
[HttpGet]
public HttpResponseMessage GetAllMessages()
{
try
{
//Load Data Into List
var mm = new MessageManager();
List<Message> msgs = mm.GetAllMessages();
//Convert List Into JSON
var jsonmsgs = JsonConvert.SerializeObject(msgs);
//Create a HTTP response - Set to OK
var res = Request.CreateResponse(HttpStatusCode.OK);
//Set the content of the response to be JSON Format
res.Content = new StringContent(jsonmsgs, System.Text.Encoding.UTF8, "application/json");
//Return the Response
return res;
}
catch (Exception exc)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc);
}
}
@charsoft
Copy link

charsoft commented Feb 6, 2019

THANK YOU!!! Good Lord I spent way too much time on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment