Copied to clipboard

Flag this post as spam?

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


  • Robert Mulder 79 posts 272 karma points c-trib
    Feb 25, 2014 @ 21:18
    Robert Mulder
    1

    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)

    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.

  • Robert Mulder 79 posts 272 karma points c-trib
    Feb 25, 2014 @ 23:54
    Robert Mulder
    1

    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.

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Jun 13, 2022 @ 15:33
    Simon Dingley
    0

    Thanks for this - 8 years later and I just hit this issue in a v7 site and the fix workaround was indeed to inherit from UmbracoAuthorizedApiController instead of UmbracoAuthorizedJsonController.

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Feb 26, 2014 @ 10:12
    Ismail Mayat
    2

    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

  • Robert Mulder 79 posts 272 karma points c-trib
    Feb 26, 2014 @ 11:00
    Robert Mulder
    0

    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 :)

  • Nikola Romcevic 26 posts 139 karma points
    Aug 18, 2014 @ 15:26
    Nikola Romcevic
    0

    Hi Robert!

    Did you get your API to work? If so, do you mind sharing your findings and perhaps some code?

  • Ian Grainger 71 posts 135 karma points
    May 11, 2015 @ 16:39
    Ian Grainger
    2

    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.

  • Russell Fox 4 posts 25 karma points
    Sep 22, 2015 @ 06:57
    Russell Fox
    1

    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");
                }
            };
        }
    );
    
  • Niels Lynggaard 190 posts 548 karma points
    Jan 09, 2018 @ 15:33
    Niels Lynggaard
    0

    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

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jan 09, 2018 @ 17:02
    Dan Diplo
    0

    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.

  • Niels Lynggaard 190 posts 548 karma points
    Jan 10, 2018 @ 09:22
    Niels Lynggaard
    0

    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

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Jan 10, 2018 @ 10:13
    Dan Diplo
    1

    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

  • Ian Grainger 71 posts 135 karma points
    Jan 10, 2018 @ 09:27
    Ian Grainger
    0

    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 :)

Please Sign in or register to post replies

Write your reply to:

Draft