Has anyone been able to make a habitat-compatible razor view engine?

Basically I'd like something that can dynamically search

"~/Views/{<Relative folder name>}/{0}.cshtml"

For example if my project is Sitecore.Foundation.Test and I have:


And then Test.cshtml calls RenderPartial("_Test")

The default razor engine doesn't work for this scenario when the "Test.cshtml" is added as a view rendering, and the page has a controller rendering with a different controller name.


1 Answer 1


Yes, you can create your custom View Engine to resolve relative paths based on the assembly name.

So, following this guide: https://weblogs.asp.net/imranbaloch/view-engine-with-dynamic-view-location

You can add a path like that:


And override the following methods:

protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        var featureName = controllerContext.Controller.GetType().Assembly.GetName().Name;
        return base.CreatePartialView(controllerContext, partialPath.Replace("%feature", featureName));

protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)

protected override bool FileExists(ControllerContext controllerContext, string virtualPath)

This will resolve to ~/Views/Sitecore.Foundation.Test/Test.cshtml

You can play around with index/substring of . to get it to just feature name.

  • I'm also using RazorGenerator.MsBuild, is it possible to get something to work with that too? Commented Mar 6, 2017 at 22:42
  • That would be a separate question @gibbocool
    – Mark Cassidy
    Commented Mar 6, 2017 at 23:37

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.