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 125 posts 366 karma points
    5 days ago
    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 1842 karma points MVP 4x c-trib
    4 days ago
    Frans de Jong
    0

    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?

Please Sign in or register to post replies

Write your reply to:

Draft