Copied to clipboard

Flag this post as spam?

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


  • Koen van Ras-Neve 56 posts 361 karma points c-trib
    Mar 11, 2019 @ 15:43
    Koen van Ras-Neve
    0

    Can't find ApplicationContext

    Hi,

    I'm trying to create a content app that inserts and gets data from a custom database table using NPoco.

    Now I've created a controller where I want to use the database, though the ApplicationContext can't be found.

    Any idea why this happens? Code below.

    using MyPlugin.Models.Rdbms;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Http;
    using Umbraco.Web.Editors;
    using Umbraco.Web.Mvc;
    
    namespace MyPlugin.Controllers
    {
        [PluginController("MyPlugin")]
        public class MyPluginBackofficeApiController : UmbracoAuthorizedJsonController
        {
            [HttpPost]
            public IHttpActionResult CreateDbData(double version)
            {
                var database = ApplicationContext.Current.DatabaseContext;
    
                var myPlugin = new MyPluginDto()
                {
                    NodeId = 123,
                    Version = version
                };
    
                return BadRequest(string.Format("Failed to insert data into the database."));
            }
        }
    }
    
  • Sebastiaan Janssen 5061 posts 15544 karma points MVP admin hq
    Mar 11, 2019 @ 15:58
    Sebastiaan Janssen
    101

    Try something like this:

    namespace MyPlugin.Controllers
    {
        [PluginController("MyPlugin")]
        public class MyPluginBackofficeApiController : UmbracoAuthorizedJsonController
        {
            private readonly IScopeProvider _scopeProvider;
    
            public MyPluginBackofficeApiController(IScopeProvider scopeProvider)
            {
                _scopeProvider = scopeProvider;
            }
    
            [HttpPost]
            public IHttpActionResult CreateDbData(double version)
            {
                var myPlugin = new MyPluginDto()
                {
                    NodeId = 123,
                    Version = version
                };
    
                using (var scope = _scopeProvider.CreateScope())
                {
                    //DB Stuff
                    scope.Database.Insert<MyPluginDto>(myPlugin);
    
                    //Always complete scope
                    scope.Complete();
                }
    
    
                return BadRequest(string.Format("Failed to insert data into the database."));
            }
        }
    }
    
  • Koen van Ras-Neve 56 posts 361 karma points c-trib
    Mar 11, 2019 @ 16:36
    Koen van Ras-Neve
    0

    Thanks Sebastiaan!

    I implemented your code, now I still have an issue calling my backoffice api in Angular.. I'm using the following code which I call with a button press:

    $scope.insertToDb = function () {
                $http.post(
                    "~/Umbraco/backoffice/MyPlugin.Controllers/MyPluginBackofficeApi/CreateDbData",
                    JSON.stringify({ version: "0.2" }));
            };
    

    Once I click the button, I get a 404 not found on the url, should I use another URL to call the function?

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Mar 11, 2019 @ 18:13
    Søren Gregersen
    1

    you have to resolve "~" manually

    I would suggest defining the url like this:

    var umbracoSettings = $window.Umbraco.Sys.ServerVariables.umbracoSettings;
    var apiUrl = umbracoSettings.umbracoPath + "/backoffice/yourplugin/controller/";
    
  • Koen van Ras-Neve 56 posts 361 karma points c-trib
    Mar 12, 2019 @ 08:28
    Koen van Ras-Neve
    0

    Hi Søren,

    Thank you for the suggestion, I tried this but still get a 404 on the url.

    With your code it creates the url:

    /umbraco/backoffice/MyPlugin.Controllers/MyPluginApi/CreateDbData
    

    MyPlugin.Controllers is the namespace of my controllers, MyPluginApiController is tha controller and CreateDbData is the method.

    Any idea why I still get a 404?

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Mar 12, 2019 @ 08:49
    Søren Gregersen
    1

    If you are using the code from your first post, the controller is named MyPluginBackofficeApiController. The routing just removes "Controller" from the end of the classname, thus making your url:

    /umbraco/backoffice/MyPlugin/MyPluginBackofficeApi/CreateDbData

    • /umbraco - the path for umbraco, from settings
    • /backoffice - area name for all backoffice controllers
    • /MyPluigin - area name provided in the PluginController-attribute
    • /MyPluginBackofficeApi - the name of the controller, with "Controller" removed
    • /CreateDbData - the name of your action (it only accepts POST-requests)

    hth :)

  • Koen van Ras-Neve 56 posts 361 karma points c-trib
    Mar 12, 2019 @ 09:29
    Koen van Ras-Neve
    0

    Thanks, this worked!

    I find it a bit weird tho that I have to use MyPlugin in stead of MyPlugin.Controllers sinces that's the namespace of my API Controllers.

  • Sebastiaan Janssen 5061 posts 15544 karma points MVP admin hq
    Mar 13, 2019 @ 09:32
    Sebastiaan Janssen
    2

    This is how it always has worked and this is how MVC works, the namespace isn't used anywhere in routes, just the controller name (removing Controller from the route name).

  • Koen van Ras-Neve 56 posts 361 karma points c-trib
    Mar 13, 2019 @ 09:34
    Koen van Ras-Neve
    0

    Thanks for the clarification, I didn't know this :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies