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

Razor view engine is hard to customize #948

Closed
talmroth opened this issue Aug 7, 2014 · 3 comments
Closed

Razor view engine is hard to customize #948

talmroth opened this issue Aug 7, 2014 · 3 comments

Comments

@talmroth
Copy link

talmroth commented Aug 7, 2014

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;
        }
    }

}
@ghstahl
Copy link

ghstahl commented Aug 8, 2014

duplicate of #817

@yishaigalatzer
Copy link
Contributor

This looks like a dup, but it highlights more issues with the design, where here the user wants more token replacements.

We will consider a single fix for both issues.

@pranavkm
Copy link
Contributor

pranavkm commented Oct 1, 2014

The changes made as part of ViewLocationExpanders largely covers the scenarios that you're trying to get to. ViewLocationFormats and AreaViewLocationFormats have also been made virtual in case you want to use the traditional route of subtyping the view engine although we recommend using expanders instead.

#1212 is meant for tracking some additional enhancement to the changes in #1039.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants