With the Glass version 5+, you need to convert the controller base class from Glass Controller to Controller(.NET class). Then you need to use IMVContext to get items/context from Sitecore, I used IMvcContext when I am working with MVC. your code will look like below -
IMvcContext mvcContext = new MvcContext();
var model = mvcContext.GetDataSourceItem<IContent_Item>();
A better approach is to register all of these new Glass services with the Sitecore dependency injection container. Use the below code to register all Glass Mapper services with the Sitecore container -
public class GlassMapperConfigurator : IServicesConfigurator
{
public void Configure(IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<Func<Database, ISitecoreService>>(_ => CreateSitecoreService);
serviceCollection.AddScoped(_ => CreateSitecoreContextService());
serviceCollection.AddScoped(_ => CreateRequestContext());
serviceCollection.AddScoped(_ => CreateGlassHtml());
serviceCollection.AddScoped(_ => CreateMvcContext());
serviceCollection.AddSingleton<Func<ISitecoreService>>(_ => Get<ISitecoreService>);
serviceCollection.AddSingleton<Func<IRequestContext>>(_ => Get<IRequestContext>);
serviceCollection.AddSingleton<Func<IGlassHtml>>(_ => Get<IGlassHtml>);
serviceCollection.AddSingleton<Func<IMvcContext>>(_ => Get<IMvcContext>);
}
private static ISitecoreService CreateSitecoreService(Database database)
{
return new SitecoreService(database);
}
private static ISitecoreService CreateSitecoreContextService()
{
var sitecoreServiceThunk = Get<Func<Database, ISitecoreService>>();
return sitecoreServiceThunk(Sitecore.Context.Database);
}
private static T Get<T>()
{
return ServiceLocator.ServiceProvider.GetService<T>();
}
private static IRequestContext CreateRequestContext()
{
return new RequestContext(Get<ISitecoreService>());
}
private static IGlassHtml CreateGlassHtml()
{
return new GlassHtml(Get<ISitecoreService>());
}
private static IMvcContext CreateMvcContext()
{
return new MvcContext(Get<ISitecoreService>(), Get<IGlassHtml>());
}
}
Then patch it like this -
<configuration>
<sitecore>
<services>
<configurator type="YourAssembly.GlassMapperConfigurator, YourAssembly" />
</services>
</sitecore>
</configuration>
finally, your code will be like this -
public YourController : Controller
{
private readonly IMvcContext _mvcContext;
public YourController (IMvcContext mvcContext)
{
var _mvcContext = mvcContext ?? throw new ArgumentNullException(nameof(mvcContext));
}
public ActionResult YourAction()
{
var model = mvcContext.GetDataSourceItem<IContent_Item>();
return View(model);
}
}
Here you don't need to Function the IMvcContext
and can use it directly.