Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Razor view engine is hard to customize #948

@talmroth

Description

@talmroth

In MVC5 we created a custom view engine that had some customizations to allow for custom view location formats. In MVC we were able to change the strings for the patterns but still had to do additional work to to process them. In MVC6 everything looks to be private/readonly and we cannot do the same customization.

here is our MVC5 implementation:

using System.Web.Mvc;

namespace CustomViewEngine
{
    //THIS VIEW ENGINE DOES NOT HANDLE AREAS.
    internal sealed class CustomLookupViewEngine : RazorViewEngine
    {
        public CustomLookupViewEngine()
        {
            ViewLocationFormats = new[]
            {
                "~/Views/%1/{1}/{0}.cshtml",
                "~/Views/%1/Shared/{0}.cshtml",
                "~/Views/Default/{1}/{0}.cshtml",
                "~/Views/Default/Shared/{0}.cshtml",
            };

            MasterLocationFormats = new[]
            {
                "~/Views/%1/{1}/{0}.cshtml",
                "~/Views/%1/Shared/{0}.cshtml",
                "~/Views/Default/{1}/{0}.cshtml",
                "~/Views/Default/Shared/{0}.cshtml",
            };

            PartialViewLocationFormats = new[]
            {
                "~/Views/%1/{1}/{0}.cshtml",
                "~/Views/%1/Shared/{0}.cshtml",
                "~/Views/Default/{1}/{0}.cshtml",
                "~/Views/Default/Shared/{0}.cshtml",
            };
        }

        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            var siteCode = GeValue(controllerContext, "siteCode");
            return base.CreatePartialView(controllerContext, partialPath.Replace("%1", siteCode));
        }


        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            var siteCode = GeValue(controllerContext, "siteCode");
            return base.CreateView(controllerContext, viewPath.Replace("%1", siteCode), masterPath.Replace("%1", siteCode));
        }


        protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
        {
            var siteCode = GeValue(controllerContext, "siteCode");
            return base.FileExists(controllerContext, virtualPath.Replace("%1", siteCode));
        }

        private static string GeValue(ControllerContext controllerContext, string key)
        {
            var result = ValueProviderFactories.Factories.GetValueProvider(controllerContext).GetValue(key);
            return result == null ? null : result.AttemptedValue;
        }
    }

}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions