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."));
}
}
}
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:
var umbracoSettings = $window.Umbraco.Sys.ServerVariables.umbracoSettings;
var apiUrl = umbracoSettings.umbracoPath + "/backoffice/yourplugin/controller/";
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:
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).
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.
Try something like this:
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:
Once I click the button, I get a 404 not found on the url, should I use another URL to call the function?
you have to resolve "~" manually
I would suggest defining the url like this:
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:
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?
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
hth :)
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.
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).Thanks for the clarification, I didn't know this :)
is working on a reply...