If I follow the instructions to the letter (by literally copying the described PersonApiController and putting it in the App_Code folder, the url mentioned in the tutorial (/umbraco/My/PersonApi/GetAll) yields a 404. (By just using it in the browser as mentioned in the tutorial)
Filled with hope I changed the url to /umbraco/backoffice/My/PersonApi/GetAll but alas, now I get a 417 Missing Token error message.
I have never heard of a 417 response code, so I have no idea what to do next. So any help would be greatly appreciated.
Here's the full code of my PersonApiController.cs (in the App_Code folder)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace My.Controllers {
[Umbraco.Web.Mvc.PluginController("My")]
public class PersonApiController : Umbraco.Web.Editors.UmbracoAuthorizedJsonController {
public IEnumerable<Person> GetAll() {
return new List<Person> {
new Person {
Id = 10,
Name = "Name",
Town = "Town",
Country = "Country"
}
};
}
}
public class Person {
public int Id { get; set; }
public string Name { get; set; }
public string Town { get; set; }
public string Country { get; set; }
}
}
I have restarted IIS, cleared Cache, made sure I'm logged in (it is an AuthorizedController after all) but all to no avail.
To provide some additional info. The code sample does work in the browser if the controller base class is changed to UmbracoAuthorizedApiController (as opposed to UmbracoAuthorizedJsonController)
Apparently the culprit is the ValidateAngularAntiForgeryToken attribute on the UmbracoAuthorizedJsonController. I assume this attribute will actually work on Angular requests and just prevents the requests from working directly from the browser (as the tutorial states it should work)
As an aside, I noticed the UmbracoAuthorizedJsonController also has the AngularJsonOnlyConfiguration attribute which changes the returned JSON code to have some Angular specific code added to it (incidentally making it invalid regular JSON). Which strikes me as a bit odd, considering that the name UmbracoAuthorizedJsonController gives no clear indication that the output would be anything other than regular valid JSON.
If any power user can either explain to me how to make the controller as described in the tutorial at http://umbraco.github.io/Belle/#/tutorials/Add-ServerSide-Data work in the browser (or perhaps update the tutorial to not state that you can simply view the controller results in the browser) that'd be great.
Thanks for this - 8 years later and I just hit this issue in a v7 site and the fixworkaround was indeed to inherit from UmbracoAuthorizedApiController instead of UmbracoAuthorizedJsonController.
To be quite frank I didn't really want a UmbracoAuthorizedJsonController per se. But because I'm new to Umbraco I figured I'd follow the steps as they are described in the tutorial linked above. In that tutorial they say I should use the UmbracoAuthorizedJsonController and can check the results in the browser.
Since that didn't work I figured I'd ask around. Perhaps someone that reads this and has the required privileges can update the text of the tutorial.
Thanks for the links though, they've showed me yet more things I can look into :)
for anyone else following the tutorial and getting here, this worked for me(Umbraco7.2.8):
If you browse to the url, you may need to view source to see the results
//adds the resource to umbraco.resources module:
angular.module('umbraco.resources').factory('personResource',
function ($q, $http) {
//the factory object returned
return {
//this cals the Api Controller we setup earlier
getAll: function () {
return $http.get("backoffice/My/PersonApi/GetAll");
}
};
}
);
I've used UmbracoAuthorizedJsonController from Angular quite a bit and haven't really had any issues like this. If it's any helper there's a full example for my AuditLogViewer on GitHub you can look at.
Also, since I haven't messed around with Angular in a long time, I'm a bit rusty, so I needed to reference my resource properly in the package.manifest.
All is well, and I'm able to see the request come through with nice json in my dev-tool once rendering my propertyeditor in the umbraco backend.
Hope it was some help and you certainly don't suck!
I actually remember now having the same issue - requests made via the browser don't work. Though the base standard ASP.NET web-api can have similar strange behaviour, as if you make a request via the browser you get back XML but from JS you get JSON etc. All can be very confusing.
If you're not familiar with it already I'd recommended using something like Postman to make your requests as it makes it a lot easier to visualise results, save tests etc. - https://www.getpostman.com/
TBH this is the problem we have found quite a lot with Umbraco. It does everything great, but the documentation is hard to follow and often out of date/incorrect.
I'm sure this is a normal problem with open source software - but if I could ask for one thing from Umbraco it wouldn't be shiny new features, it'd be easy to find and up-to-date documentation :)
Creating an UmbracoAuthorizedJsonController
Hello all,
I'm trying to create a property editor with server side data as described in this tutorial. But I can't get it to work.
http://umbraco.github.io/Belle/#/tutorials/Add-ServerSide-Data
If I follow the instructions to the letter (by literally copying the described PersonApiController and putting it in the App_Code folder, the url mentioned in the tutorial (/umbraco/My/PersonApi/GetAll) yields a 404. (By just using it in the browser as mentioned in the tutorial)
Looking around on this forum I found a link to this page http://umbraco.com/follow-us/blog-archive/2014/1/17/heads-up,-breaking-change-coming-in-702-and-62.aspx stating that URLs will change for exactly these kinds of controllers.
Filled with hope I changed the url to /umbraco/backoffice/My/PersonApi/GetAll but alas, now I get a 417 Missing Token error message.
I have never heard of a 417 response code, so I have no idea what to do next. So any help would be greatly appreciated.
Here's the full code of my PersonApiController.cs (in the App_Code folder)
I have restarted IIS, cleared Cache, made sure I'm logged in (it is an AuthorizedController after all) but all to no avail.
To provide some additional info. The code sample does work in the browser if the controller base class is changed to UmbracoAuthorizedApiController (as opposed to UmbracoAuthorizedJsonController)
Apparently the culprit is the ValidateAngularAntiForgeryToken attribute on the UmbracoAuthorizedJsonController. I assume this attribute will actually work on Angular requests and just prevents the requests from working directly from the browser (as the tutorial states it should work)
As an aside, I noticed the UmbracoAuthorizedJsonController also has the AngularJsonOnlyConfiguration attribute which changes the returned JSON code to have some Angular specific code added to it (incidentally making it invalid regular JSON). Which strikes me as a bit odd, considering that the name UmbracoAuthorizedJsonController gives no clear indication that the output would be anything other than regular valid JSON.
If any power user can either explain to me how to make the controller as described in the tutorial at http://umbraco.github.io/Belle/#/tutorials/Add-ServerSide-Data work in the browser (or perhaps update the tutorial to not state that you can simply view the controller results in the browser) that'd be great.
Thanks for this - 8 years later and I just hit this issue in a v7 site and the
fixworkaround was indeed to inherit fromUmbracoAuthorizedApiController
instead ofUmbracoAuthorizedJsonController
.Robert,
Not sure why you want umbracoauthorisedjsoncontroller but if you look at code for similarity i have json controller that is authorised see
https://bitbucket.org/ismailmayat/similarity/src/daa1d22777a7a940ea2105697c0e812aa4c3ae28/src/SimilarityApiController.cs?at=master
and to ensure json sent back i have
https://bitbucket.org/ismailmayat/similarity/src/daa1d22777a7a940ea2105697c0e812aa4c3ae28/src/Events/ApplicationStartUp.cs?at=master
see line 22
Regards
Ismail
To be quite frank I didn't really want a UmbracoAuthorizedJsonController per se. But because I'm new to Umbraco I figured I'd follow the steps as they are described in the tutorial linked above. In that tutorial they say I should use the UmbracoAuthorizedJsonController and can check the results in the browser.
Since that didn't work I figured I'd ask around. Perhaps someone that reads this and has the required privileges can update the text of the tutorial.
Thanks for the links though, they've showed me yet more things I can look into :)
Hi Robert!
Did you get your API to work? If so, do you mind sharing your findings and perhaps some code?
How in the actual heck is this not sorted, yet? This is the main tutorial on how to make server-side data accessible to editors, and it doesn't work!?
I've also just followed it and got to the 404. :(
It turns out the only problem is as @Robert said - the path in the tutorial should be umbraco/backoffice/my/PersonApi/.
Looks like: https://our.umbraco.org/documentation/Reference/WebApi/ is also incorrect.
for anyone else following the tutorial and getting here, this worked for me(Umbraco7.2.8):
If you browse to the url, you may need to view source to see the results
I also followed that tutorial and ended up in the exact same spot. I DID however figure out that the URL for the service changed to /backoffice/..
But I'm also getting the 417 http error (token).
I had a good look at Ismail's code and changed my controller to UmbracoAuthorizedApiController instead of the UmbracoAuthorizedJsonController
I guess I'll be going in that direction then..
But surely there must be a way to make this work with like intended? Anyone?
Otherwize it would be cool if somebody could update the tutorial with actual working code.. :/
Cheers, Niels
I've used
UmbracoAuthorizedJsonController
from Angular quite a bit and haven't really had any issues like this. If it's any helper there's a full example for my AuditLogViewer on GitHub you can look at.Hi Dan!
Thank you for replying, nice to take a look at someone else's code sometimes :)
It turns out, that the tutorial is wrong, when it states that you can request the controller action via your browser, even when logged in to Umbraco. (As this post pointed me at; https://our.umbraco.org/forum/extending-umbraco-and-using-the-api/83739-umbracoauthorizedjsoncontroller-417-missing-token)
Also, since I haven't messed around with Angular in a long time, I'm a bit rusty, so I needed to reference my resource properly in the package.manifest.
All is well, and I'm able to see the request come through with nice json in my dev-tool once rendering my propertyeditor in the umbraco backend.
Thanx for your help, #H5ISuck! :)
Cheers, Niels
Hope it was some help and you certainly don't suck!
I actually remember now having the same issue - requests made via the browser don't work. Though the base standard ASP.NET web-api can have similar strange behaviour, as if you make a request via the browser you get back XML but from JS you get JSON etc. All can be very confusing.
If you're not familiar with it already I'd recommended using something like Postman to make your requests as it makes it a lot easier to visualise results, save tests etc. - https://www.getpostman.com/
NB. It's possible to edit the Umbraco docs and make a pull-request as they're all on GitHub: https://github.com/umbraco/UmbracoDocs
TBH this is the problem we have found quite a lot with Umbraco. It does everything great, but the documentation is hard to follow and often out of date/incorrect.
I'm sure this is a normal problem with open source software - but if I could ask for one thing from Umbraco it wouldn't be shiny new features, it'd be easy to find and up-to-date documentation :)
is working on a reply...