Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom 713 posts 954 karma points
    Sep 16, 2013 @ 01:51
    Tom
    0

    ApiController Return Json

    Hi Guys,

    I've got a simple api controller like so:

    public class MailingListApiController : UmbracoApiController

    {

    public string GetAllRecipients()

    {

    return Json.Encode(new string[] { "Table", "Chair", "Desk", "Computer", "Beer fridge" });

    }

    }

     

     

     

    in my app_start folder I have webapiconfig.cs:

    public static class WebApiConfig

    {

    public static void Register(HttpConfiguration config)

    {

    config.Routes.MapHttpRoute(

    name: "DefaultApi",

    routeTemplate: "api/{controller}/{id}",

    defaults: new { id = RouteParameter.Optional }

    );

     

    var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");

    config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

    }

    }

     

     

    which removes the XML formatter

     

    i have checked that it is called in global.asax

     

    But my controller is still returning xml.. just wondering if anyone could suggest how to get this working..

    Cheers

  • Sasha Kruglyak 10 posts 97 karma points
    Nov 11, 2014 @ 09:54
    Sasha Kruglyak
    0

    Hi,

     

    try this:

     

    public HttpResponseMessage ControlleName()

    {

    string json = "some content";

    return new HttpResponseMessage() { Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json") };

    }

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies