Copied to clipboard

Flag this post as spam?

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


  • Ross Ekberg 130 posts 381 karma points
    Nov 27, 2024 @ 22:23
    Ross Ekberg
    0

    Umbraco Back Office Routing

    Umbraco 10

    I am trying to extend the back office. I want to communicate with a custom database. I keep getting the following error:

    Request error: The URL returned a 404 (not found): backoffice/Test/GetDatabases

    I have tried several different things. Nothing works. Below is my code.

    Umbraco controller:

    namespace Umbraco_Prod.Controllers
    {
        [Route("[controller]/[action]")]
        public class TestController : UmbracoAuthorizedApiController
        {
    
    
            public class ESource
            {
                public int DBID { get; set; }
                public string LinkText { get; set; }
                public string LinkURL { get; set; }
                public string LtrLink { get; set; }
                public string Description { get; set; }
                public List<Category> CatArray { get; set; }
                public List<string> AvailArray { get; set; }
            }
    
            [HttpGet]
            public JsonResult GetDatabases()
            {
    
                DPL_Databases_TESTContext dc = new DPL_Databases_TESTContext();
    
                var displayItems = dc.DatabaseLists.Select(x => new ESource()
                {
                    DBID = x.Dbid,
                    LinkText = x.LinkText,
                    LinkURL = x.LinkUrl,
                    Description = x.Description
                }).OrderBy(x => x.LinkText).ToList();
    
                return new JsonResult(displayItems);
    
            }
        }
    }
    

    AngularJS controller:

    angular.module('umbraco').controller('TestController', function ($scope, testResource) {
        testResource.getDatabases().then(function (response) {
            $scope.databases = response.data;
        });
    });
    

    AngularJS Resource:

    angular.module('umbraco.resources').factory('testResource',
        function ($q, $http, umbRequestHelper) {
            // the factory object returned
            return {
                // this calls the ApiController we setup earlier
                getDatabases: function () {
                    return umbRequestHelper.resourcePromise(
                        $http.get("backoffice/Test/GetDatabases"),
                        "Failed to retrieve all Person data");
                }
            };
        }
    );
    

    Dashboard:

    <body>
        <table ng-controller="TestController">
            <thead>
                <tr>
                    <th>Name</th>
                </tr>
            </thead>
            <tr ng-repeat="db in databases">
                <td>{{ db.LinkText }}</td>
            </tr>
        </table>
    </body>
    
  • Frans de Jong 550 posts 1862 karma points MVP 4x c-trib
    Nov 28, 2024 @ 10:49
    Frans de Jong
    100

    I think you are mixing newer documentation with an older Umbraco version.

    If you look at this documentation: https://docs.umbraco.com/umbraco-cms/10.latest/reference/routing/umbraco-api-controllers

    UmbracoApiControllers get routing added automaticly.

    The default route for the UmnbracoAuthorizedApiController is: ~/Umbraco/backoffice/Api/[YourControllerName]

    Or if you add the plugincontroller attribute: ~/Umbraco/backoffice/[YourAreaName]/[YourControllerName]

    So if you remove the Route attribute the default Url in your case will be: /umbraco/backoffice/api/test/getdatabases

    Can you please let me know if this fixed your issue?

  • Ross Ekberg 130 posts 381 karma points
    Dec 05, 2024 @ 15:26
    Ross Ekberg
    0

    That seemed to do the trick. I can navigate to that specific URL and I get back the json data. I am now running into some other error, though, that is preventing the data from displaying. not sure what it is. It's probably unrelated to this question though.

    Thanks for the help!

Please Sign in or register to post replies

Write your reply to:

Draft