There are certain circumstances when working with Episerver or Episerver commerce when you need to come off the beaten track and interact with the HTTP context a bit more closely. For example, if you need to create a custom ActionFilterAttribute, or, you need to to do some routing wizardry with communication between blocks on your product pages. One of my most popular tutorials EPiServer 7 : How do I get the “CurrentPage” from a controller ? deals with a similar principle.

The code below assumes that you are working with-in an ActionFilterAttribute but as long as you have access to the controller context the same principles apply to wherever.

Getting EntryContentBase from the HTTP context

The first approach is the generic approach that will get you access to your product code. This involves getting the currentContent out of the RouteData and typing it to an EntryContentBase. Using this code will allow you to process products and variants at the same time.

Getting the Variant from the HTTP context

If you want your code to be more specific to the data you are working on, then you simply type it to the default Episerver objects VariationContentor ProductContent. In most scenarios you will be working with your own commerce types as you would usually

Conclusion

In today’s tutorial, we’ve covered how to commerce products and variations directly out of the HTTP context/controller context. We use the currentContent key in the RouteData table and then type it to the object you care about. The code is pretty simple and straightforward but can be a bit tricky at times to figure out yourself.

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Share this entry

http://jondjones.com/wp-content/uploads/2013/06/bannerepitutorial.jpg4561595Jon D Joneshttp://jondjones.com/wp-content/uploads/2013/04/logo.pngJon D Jones2016-01-27 11:18:262016-02-03 15:31:32EPiServer Commerce : How do I get the current Product or Variant from a controller or attribute ?

MY EPISERVER BOOK

EPISERVER NEWSLETTER

Connect

Hi, I'm Jon, I write articles about creating and optimizing websites as well as making money from your online business. I am technical architect and technology fanatic by profession. You can find out more about me by signing up to my newsletter.