Copied to clipboard

Flag this post as spam?

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


  • Matthew Kirschner 323 posts 611 karma points
    Feb 26, 2015 @ 19:51
    Matthew Kirschner
    0

    Return a JsonResult

    I'm using a jQuery Datatables editor that is expecting a JsonResult object from my controller. I want to call the Json() method within my UmbracoApiController, but this method doesn't exist on UmbracoApiController like it does with the standard ApiController.

    Does the Umbraco API provide an alternative?

  • Sören Deger 733 posts 2844 karma points c-trib
    Feb 26, 2015 @ 19:54
    Sören Deger
    1

    Hi Matthew,

    try to use UmbracoAuthorizedJsonController instead of UmbracoApiController. I had use this a long time ago and I think it has worked with this. Maybe I'm wrong but also, just try it ;)
     

    Best,

    Sören

     

  • Matthew Kirschner 323 posts 611 karma points
    Feb 26, 2015 @ 20:12
    Matthew Kirschner
    0

    Thanks for the swift response, Soren.

    Unfortunately, UmbracoAuthorizedJsonController does not expose the Json method either : (

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 26, 2015 @ 20:26
    Jan Skovgaard
    0

    Hi Matthew

    I might be way of here - But have you seen the documentation on WebApi? https://our.umbraco.org/documentation/Reference/WebApi/ - Could that help? Or am I missing the point? :)

    /Jan

  • Matthew Kirschner 323 posts 611 karma points
    Feb 26, 2015 @ 20:40
    Matthew Kirschner
    0

    Hi, Jan.

    I've read through that doc a few times but don't see any reference to alternatives for methods that would normally be on ApiControllor (specifically, ApiController.Json).

    Here's the gist of what the working controller should look like:

    public ActionResult Patents([FromBody] FormDataCollection formData)
    {
        //Custom logic that generates an object from my model
        var response = GenerateObjectFromModel();
    
        return Json(response);
    }
    

    This would work if my controller inherits from ApiController, but UmbracoApiController doesn't seem to have the same Json method that turns an object into a JsonResult.

  • Richard Terris 273 posts 715 karma points
    Feb 27, 2015 @ 12:07
    Richard Terris
    0

    Matthew,

    UmbracoApiController inherits from ApiController which means you have access to all fields and methods from both in your controller.

    I don't see a JsonResult or Json object on either of these controllers, but it looks like that object lives in Sytem.Web.Mvc: https://msdn.microsoft.com/en-us/library/system.web.mvc.jsonresult%28v=vs.118%29.aspx

    If you just need to return Json you could try:

        public HttpResponseMessage GetPeople()
        {
          //get your object from wherever, database or cache...
          JavaScriptSerializer serialiser = new JavaScriptSerializer();
          HttpContext.Current.Response.ContentType = "application/json";
          HttpContext.Current.Response.Write(serialiser.Serialize(yourObject));
          return new HttpResponseMessage();
         }
    

    I'm not sure if that will work but you should be able to access JsonResult from within System.Web.Mvc provided you have a reference to it.

  • Anders Bjerner 487 posts 2990 karma points MVP 8x admin c-trib
    Jul 16, 2015 @ 19:50
    Anders Bjerner
    2

    I've created a package for this a while back that forces the controller to output JSON rather than XML or JSON depending on the accept header.

    You can read more about it on GitHub:

    https://github.com/abjerner/Skybrud.WebApi.Json

    To use the package, you would just have to add a single attribute on the class, and WebApi will then automatically convert the return value of each method into JSON.

    Here is a small example:

    using Skybrud.WebApi.Json;
    using Umbraco.Web.WebApi;
    
    namespace WebApplication1.Controllers {
    
        [JsonOnlyConfiguration]
        public class JsonTestController : UmbracoApiController {
    
            public object GetTest() {
                return new {
                    meta = new {
                        code = 200
                    },
                    data = "Yay! We have some JSON!"
                };
            }
    
        }
    
    }
    
  • Matthew Kirschner 323 posts 611 karma points
    Jul 20, 2015 @ 17:16
    Matthew Kirschner
    0

    Thank you Anders, I'll take a look at that package when I get to this later.

    For now, I'm just using a Surface Controller with the following and it works perfectly:

    return Json(response, JsonRequestBehavior.AllowGet);
    
Please Sign in or register to post replies

Write your reply to:

Draft