Copied to clipboard

Flag this post as spam?

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


  • Bunnynut 136 posts 318 karma points
    May 31, 2018 @ 14:47
    Bunnynut
    0

    What Url to use for PluginController used in custom section

    I have created a custom section called "Aanmeldingen" and added a file 'edit.html' to /umbraco/views/aanmeldingenTree.

    Next I want to retrieve some data by calling AanmeldingenController:

    public class AanmeldingenController : UmbracoAuthorizedJsonController
    {
        //dot stuff here
    }
    

    I have tried various URL's to call this method, like: umbraco/backoffice/aanmeldingen/getalleaanmeldingen backoffice/aanmeldingen/getalleaanmeldingen umbraco/aanmeldingen/getalleaanmeldingen

    and placed the file containing this code in various location (does that even matter?)

    Added a specific route to the RouteConfig.s does't help either.

    What is supposedly the correct way to do this?

  • Anders Bjerner 487 posts 2995 karma points MVP 8x admin c-trib
    May 31, 2018 @ 15:19
    Anders Bjerner
    102

    Hi Bunnynut,

    Your example above doesn't contain a PluginController attribute, when this is the case - eg with the following code:

    using Newtonsoft.Json.Linq;
    using Umbraco.Web.Editors;
    
    public class AanmeldingenController : UmbracoAuthorizedJsonController
    {
    
        public object GetAlleAanmeldingen()
        {
    
            return new JArray();
    
        }
    
    }
    

    The URL will be:

    /umbraco/BackOffice/Api/Aanmeldingen/GetAlleAanmeldingen
    

    On the order hand, if you actually do add the PluginController attribute - eg. like in the code below:

    using Newtonsoft.Json.Linq;
    using Umbraco.Web.Editors;
    using Umbraco.Web.Mvc;
    
    [PluginController("Stuff")]
    public class AanmeldingenController : UmbracoAuthorizedJsonController
    {
    
        public object GetAlleAanmeldingen()
        {
    
            return new JArray();
    
        }
    
    }
    

    the URL for your method will then be as below instead:

    /umbraco/backoffice/Stuff/Aanmeldingen/GetAlleAanmeldingen
    

    It's not necessary to add any custom routes, as Umbraco will handle the routing for you automatically.

    By the way - you can actually get Umbraco to tell you the correct URL of your method.

    If you have an MVC view (or just an instance of UrlHelper) in general, you can do something like:

    @(Url.GetUmbracoApiService<AanmeldingenController>("GetAlleAanmeldingen"))
    

    Then you'll have the correct URL, which takes the PluginController attribute, class name and method name into consideration ;)

  • Bunnynut 136 posts 318 karma points
    Jun 01, 2018 @ 08:20
    Bunnynut
    0

    Hi Anders,

    I added the PluginController-attribute and I am now able to call the method from script with backoffice/AanmeldingenPlugin/Aanmeldingen/GetAlleAanmeldingen.

    Thanks!

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    May 31, 2018 @ 17:39
    David Brendel
    0

    Hi,

    a good way to not have to fiddle with your urls in angular is to use the build in ServerVariables ServerVariablesParser.Parsing += Parsing; in a startup evemt. With that you can get the urls to your controller by using

    var mainDictionary = new Dictionary<string, object>();
    mainDictionary.Add("aanmeldingenBaseUrl", urlHelper.GetUmbracoApiServiceBaseUrl<AanmeldingenController>(controller => controller.GetAlleAanmeldingen(null)));
    

    and adding it to the dictionary provided in the method

    if (!e.Keys.Contains("myCustomName"))
    {
        e.Add("myCustomName", mainDictionary);
    }
    

    Then you can use it in your angular controller like this

    Umbraco.Sys.ServerVariables.myCustomName.aanmeldingenBaseUrl
    
Please Sign in or register to post replies

Write your reply to:

Draft