Umbraco ApplicationContext.Current is null

删除回忆录丶 提交于 2019-12-11 10:33:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!