5

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

Basically I'd like something that can dynamically search

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

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

~/Views/Test/Test.cshtml
~/Views/Test/_Test.cshtml

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

1 Answer 1

4

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:

"~/Views/%feature/{0}.cshtml"

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.

2
  • 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.