Copied to clipboard

Flag this post as spam?

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


  • Harikrishna Parmar 24 posts 198 karma points
    Mar 07, 2020 @ 10:58
    Harikrishna Parmar
    0

    How can I get ApiBaseUrl or backoffice API?

    How can I write or add ApiBaseUrl or backoffice API? I want to write like this :

    $http.get( umbRequestHelper.getApiUrl( "configManagerEditorApiBaseUrl", "GetByPath", { virtualPath: virtualpath }))

  • Nik 1299 posts 5501 karma points MVP 3x c-trib
    Mar 07, 2020 @ 16:23
    Nik
    0

    Hey Harikrishna,

    So, I think you are asking how you can get the url for a C# controller in your Angular in the back office. This blog post here: https://www.justnik.me/blog/accessing-config-settings-in-your-umbraco-angular-controller shows a way you can achieve this, but you will still have to add the Action name when you want to make that call.

    Nik

  • Harikrishna Parmar 24 posts 198 karma points
    Mar 08, 2020 @ 07:02
    Harikrishna Parmar
    101

    Thank you, Nik For your reply.

    But this is not compatible with Umbraco 8. I found a solution. After creating an API controller, we need to register using IComponent.

    Controller :

          [PluginController("ConfigEditorManager")]
                [UmbracoApplicationAuthorize(Constants.Applications.Settings)]
                public class ConfigEditorController : BackOfficeNotificationsController
                {
            public string GetByPath(string virtualPath)
                    {
        var data=virtualPath;
            return data;
            }
    }
    

    After creating controller, we need to register it using the component :

    public class ConfigManagerComponent : IComponent
    {
    public void Initialize()
            {
    
                ServerVariablesParser.Parsing += ServerVariablesParser_Parsing;
            }
            private void ServerVariablesParser_Parsing(object sender, System.Collections.Generic.Dictionary<string, object> e)
            {
    if (HttpContext.Current == null) throw new InvalidOperationException("HttpContext is null");
    
                if (e.ContainsKey("configManager")) return;
                var urlHelper = new UrlHelper(new RequestContext(new HttpContextWrapper(HttpContext.Current), new RouteData()));
                e["configManager"] = new Dictionary<string, object>
                {
                    {"configManagerEditorsBaseUrl", urlHelper.GetUmbracoApiServiceBaseUrl<ConfigEditorController>(controller => controller.GetByPath(null))}
                };
    }
    }
    

    Then, this Component also needs to register in composer :

    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class ConfigManagerComposer : IUserComposer
        {
            public void Compose(Composition composition)
            {
                composition.Components().Append<ConfigManagerComponent>();
            }
        }
    

    Accessing your variables in angularjs :

    (function () {
        'use strict';
    
        function myCustomController($scope, $http) {
            var apiUrl = umbRequestHelper.getApiUrl(
                        "configManagerEditorsBaseUrl", "GetByPath");
    
            console.log(apiUrl);
        }
        angular.module('umbraco').controller('myCustomController', myCustomController);
    
    }
    
  • Nik 1299 posts 5501 karma points MVP 3x c-trib
    Mar 08, 2020 @ 09:21
    Nik
    0

    Hi Harikrishna,

    Pleased to see you got it working, yes the blog post was for v7, but it would work on v8, except replacing the ApplicationEventsHandler class with Components and Composers like you have. The important bit is the hook into the ServerVariablesParser event to add your own entry :-)

    I do, at some point, need to write a v8 version of that blog post :-)

    Nik

  • Harikrishna Parmar 24 posts 198 karma points
    Mar 08, 2020 @ 09:30
    Harikrishna Parmar
    0

    Thank you, Nik.

  • Ebin 9 posts 78 karma points
    Mar 09, 2020 @ 12:25
    Ebin
    0

    Hi,

    Use this code in API controller

    UmbracoHelper uh = new UmbracoHelper(UmbracoContext.Current);

                IPublishedContent content = uh.TypedContent(node.Id);
    
    
                string url =content.UrlWithDomain()
    
  • Nik 1299 posts 5501 karma points MVP 3x c-trib
    Mar 09, 2020 @ 12:43
    Nik
    0

    Hi Ebin,

    That code doesn't give you any API urls, it gives the url of a published content page. In addition it looks like it's Umbraco v7 code and not v8 code as the approach for getting Content in v8 is different.

    As an FYI, it's bad practice to be creating your own UmbracoHelper instances if you can help it. For example in an API Controller there is already an Umbraco property which is a helper (assuming your controller inherits from one of the Umbraco base classes).

    Thanks

    Nik

Please Sign in or register to post replies

Write your reply to:

Draft