Home > Blockchain >  customizing cshtml for each customer
customizing cshtml for each customer

Time:11-22

I'm using .Net Framework 4.8. I need an option in my web app to be able to override default views (just cshtml files) for each customer we have. For example consider that I have a view in folder views/account/Login.cshtml. I want to load a customized version if exists from /Views/Overrides/[MyCustomerName]/Account/Login.cshtml. How can I achieve that?

CodePudding user response:

If each of your customer is having a separate instance installed (If I understand correctly! Basically I recommend to handle these things in CI/CD pipelines.) ,you can do this: First create and derive a class from RazorViewEngine:

public class MultiImplementationRazorViewEngine : RazorViewEngine
    {

        private static string _currentImplementation = /*Link to a config file to have your */;

        public MultiImplementationRazorViewEngine()
            : this(_currentImplementation)
        {
        }

        public MultiImplementationRazorViewEngine(string impl)
        {
            SetCurrentImplementation(impl);
        }

        public void SetCurrentImplementation(string impl)
        {
            if (string.IsNullOrWhiteSpace(impl))
            {
                this.ViewLocationFormats = new string[] {
                @"~/Views/{1}/{0}.cshtml",
                @"~/Views/Shared/{0}.cshtml"};
            }
            else
            {
                _currentImplementation = impl;
                ICollection<string> arViewLocationFormats =
                     new string[] { "~/Views/Overrides/"   impl   "/{1}/{0}.cshtml" };
                ICollection<string> arBaseViewLocationFormats = new string[] {
                @"~/Views/{1}/{0}.cshtml",
                @"~/Views/Shared/{0}.cshtml"};
                this.ViewLocationFormats = arViewLocationFormats.Concat(arBaseViewLocationFormats).ToArray();
            }
        }

        public static string CurrentImplementation
        {
            get { return _currentImplementation; }
        }

    }

Then in startup.cs add these lines to replace the razor engine:

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new MultiImplementationRazorViewEngine());
  • Related