问题
I have an MVC website and I need to pull out a couple of pieces of data from an Umbraco database. I don't need any of the Umbraco views or any of that stuff. I'm new to the Umbraco Core libraries.
What I did was create a reference to the Umbraco.core dll from my web project and added the connection string to the Umbraco database in my web config. I then added a method to attempt to retrieve some data
public IContentType GetBenefits() {
var contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
var benefits = contentTypeService.GetContentType("Benefits");
return benefits;
}
ApplicationContext.Current is always null
I'm running on my local dev PC under IIS Express
Do I need to do anything else to instantiate the Umbraco connection?
回答1:
ApplicationContext.Current
will always be null in this case because ApplicationContext.Current
refers to the current running instance of an Umbraco Application. In order to have a running instance you need a full Umbraco installation configured, bootstrapped and running.
I don't think you will be able to do this with Umbraco.Core
alone and adding all those extra libraries for the sake of connecting to a database is overkill anyway.
If you need to access Umbraco specific database content then the best option is to Add an Umbraco Api Controller to the actual Umbraco instance and expose the data that you need with Web API.
At a push you could connect to the database directly but the database is well abstracted and normalized so, unless you need only very specific data from the database, this isn't going to be worth the effort.
来源:https://stackoverflow.com/questions/51317572/umbraco-applicationcontext-current-is-null