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.
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 ;)
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.
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.
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!"
};
}
}
}
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?
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
Thanks for the swift response, Soren.
Unfortunately, UmbracoAuthorizedJsonController does not expose the Json method either : (
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
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:
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.
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:
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.
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:
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:
is working on a reply...