Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

How to tell if request is in preview mode with episerver 10

Vote:
 

Hi,

i am working on upgrading a site from version 7.5 to version 10 and i'm struggling to work out a solution to an error i am getting. There is some code that is used to find out if the request is in preview mode which was working in 7.5 but doesn't work in version 10:

EPiServer.Web.Routing.Segments.RequestSegmentContext.CurrentContextMode == ContextMode.Preview

the problem is that RequestSegmentContext doesn't exist in EPiServer.Web.Routing.Segments in version 10 (http://world.episerver.com/documentation/class-library/?documentId=cms/10/549E82FF) so this code won't build. I have had a play around with some other options and searched online but i can't work out the correct way to determine if the request is in preview mode.

this seems like a simple & common bit of code so i'm wondering if anyone has found a way to do this in episerver 10?

cheers

Sam

#171792
Nov 17, 2016 15:43
Vote:
 

Hi Sam,

A lot of undocumented implementations were moved into sub-namespaces (.Internal) with EPiServer 10.
See http://world.episerver.com/documentation/Release-Notes/ReleaseNote/?releaseNoteId=CMS-3762

The same feature should still be available and usable under the new namespace:
EPiServer.Web.Routing.Segments.Internal.RequestSegmentContext.CurrentContextMode

Cheers,
//Patrick

#171810
Edited, Nov 17, 2016 20:21
Vote:
 

Depending on where you wanna use this code, there is an extension method called GetContextMode on RequestContext. RequestContext is usually available, so I would recommend using that method instead since it's not an internal API. Please see http://world.episerver.com/documentation/Class-library/?documentId=cms/10/D74C6A77

#171814
Nov 17, 2016 22:13
fuji - Oct 28, 2019 16:11
Thank you for the answer, however I've struggled a bit trying to understand it and then this worked for me - ControllerContext.RequestContext.GetContextMode(); - but it returns "Edit" while in "Preview" mode.

Cheers!
Vote:
 

requestContext.GetContextMode() is exactly what i was looking for.

Thanks for your help Johan!

#171819
Nov 18, 2016 10:06
Vote:
 

There is also an abstraction EPiServer.Web.IContextModeResolver registered in IOC container that can be used.

#171851
Nov 18, 2016 14:54
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.