0

I'm trying to access Sitecore 10.2 model fields using Glass.Mapper.Sc.92 version 5.8, and registered all Glass.Mapper services in GlassMapperConfigurator class. However facing the "Context is null" issue.

enter image description here

Created another class to register the controller

public class GlassCustomConfigurator: IServicesConfigurator
{
    public void Configure(IServiceCollection serviceCollection)
    {
        serviceCollection.AddTransient<FooterController>();
    }
}

Controller code:

public class FooterController : Controller
{
    private readonly IMvcContext _mvcContext;

    public FooterController(IMvcContext mvcContext)
    {
        _mvcContext = mvcContext;
    }
    public ActionResult Footer()
    {
        var model = _mvcContext.GetContextItem<FooterModel>();
        return View(model);
    }
}

GlassCustom.config to register the services

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <services>
            <configurator type="Test.GlassCustomConfigurator, Test" />
            <configurator type="Test.GlassMapperConfigurator, Test" />
        </services>
    </sitecore>
</configuration>

Any idea how can I fix this issue?

7
  • Is this a new Glass setup, have the Glass config files been added manually? Since the change to PackageReferences, these config files are not added automatically. Commented Apr 6, 2022 at 7:46
  • @MichaelEdwards I've added the config file to reference the classes created by me. Commented Apr 6, 2022 at 9:41
  • Can you check these files have been deployed to your sitecore instance github.com/mikeedwards83/Glass.Mapper/tree/master/Source/… Commented Apr 6, 2022 at 20:40
  • 1
    These files need to be published to your website otherwise it won't work. Annoyingly the package reference changes do not support this, so you ahve to manually add them to your solution. Commented Apr 8, 2022 at 7:50
  • 1
    @MichaelEdwards Thank you so much for the help. It worked! Commented Apr 8, 2022 at 10:27

2 Answers 2

0

Glass.Mapper.Sc requires a set of configuration files to be deployed to Sitecore:

https://github.com/mikeedwards83/Glass.Mapper/tree/master/Source/Glass.Mapper.Sc/App_Config/Include/Glass

Before the move the packageReferences, these config files would automatically be added and deployed to your Sitecore solution.

Now you will need to manually add them to your Visual Studio solution to ensure they are correctly deployed.

0

Try something like this maybe:

serviceCollection.AddScoped(_ => CreateSitecoreContextService());

and CreateSitecoreContextService method:

private static ISitecoreService CreateSitecoreContextService()
{
  var sitecoreServiceThunk = Get<Func<Database, ISitecoreService>>();
  return sitecoreServiceThunk(Sitecore.Context.Database);
}

Source:

https://www.coreysmith.co/glass-mapper-5-dependency-injection/

1
  • I tried this approach but no luck. Any other resolution to this ? Commented Apr 6, 2022 at 5:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.