1

This is using Sitecore 9.0.2 and Glass 5.1.10. I'm trying to figure out the best way to access the IMvcContext (or ISitecoreService) in a view rendering. Using controller rendering is very straightforward, but one of our devs opened the view rendering with IMvcContext mvcContext = new IMvcContext() which seems unneeded to declare a new context.

I would think there's another way to essentially get the existing context in, but I'm not sure if that's a cast of Sitecore.Context in some fashion, or if that's really the best way. Or, if it would be best practice to switch to a controller rendering if this situation comes up, which is perfectly viable.

1
  • 3
    If you need to do that, you should convert this to a controller rendering and inject the IMvcContext into the controller and use it that way. Don't try and do that in a view rendering.
    – Richard Seal
    Commented Oct 25, 2018 at 14:47

3 Answers 3

2

This should be changed to a ControllerRendering so you can do business logic (like acquiring other supporting items and assembling a view model) in the controller layer or a service layer.

1

When you mentioned Glass v5 and use IMvcContext() in a view it made me think you are not using Glass v5 correctly. The change between v4 and v5 is significant and leads to confusion if you're used to previous version or you try to search google which has only old examples. Have a look in here to see what are the changes and how to access context in a view.

1
  • I had missed that, that's another way to get the SitecoreService without a new context. Thanks! Commented Jan 16, 2019 at 21:49
1

For others who are still struggling, here is an official site link from glass mapper, I found this to be more clear than other answers.

http://www.glass.lu/Mapper/documentation/Upgrade-ToV5.html

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.