1

I am willing to override the "_RentalInfo.cshtml" and "_AddToCart.cshtml" pages from my Plugin. For this, I created the custom View Engine and provided the path to the files into "PartialViewLocationFormats, ViewLocationFormats". //CustomViewEngine Class

public class CustomViewEngine : ThemeableRazorViewEngine
{
        public CustomViewEngine()
        {
            PartialViewLocationFormats = new[] { "~/Plugins/Misc.Rental/Views/Product/{0}.cshtml" };
            ViewLocationFormats = new[] { "~/Plugins/Misc.Rental/Views/Product/{0}.cshtml" };
         }
}

Then, I created the RouteProvider.cs file within Plugin and added my "CustomViewEngine" within view engine list. I also set the priority of my route provider as highest.

//RouteProvider
public partial class RouteProvider : IRouteProvider
{
        #region Fields
        public int Priority
        {
            get
            {
                return int.MaxValue;
            }
        }
        #endregion

        #region Methods
        public void RegisterRoutes(RouteCollection routes)
        {
            ViewEngines.Engines.Add(new CustomViewEngine());

        }
        #endregion
}

Lastly, as per the path mentioned in my "CustomViewEngine", I have created the "_RentalInfo.cshtml" and "_AddToCart.cshtml" pages beneath "Views" folder within my plugin.

But, then too default NopCommerce default partial view is only being displayed. Can anyone review my code and let me know, what I have performed wrong or missing to override the partial views.

1 Answers1

1

I am using following code in my plugins

public PluginRazorViewEngine()
{
    PartialViewLocationFormats = new[] 
    {
        "~/Plugins/My.Plugin/Views/{1}/{0}.cshtml",
        "~/Plugins/My.Plugin/Views/Shared/{0}.cshtml",
    };

    ViewLocationFormats = new[] 
    {
        "~/Plugins/My.Plugin/Views/{1}/{0}.cshtml",
        "~/Plugins/My.Plugin/Views/Shared/{0}.cshtml",
    };

    AreaPartialViewLocationFormats = new[]
    {
        "~/Plugins/My.Plugin/Views/{1}/{0}.cshtml",
        "~/Plugins/My.Plugin/Views/Shared/{0}.cshtml",
    };

    AreaViewLocationFormats = new[]
    {
        "~/Plugins/My.Plugin/Views/{1}/{0}.cshtml",
        "~/Plugins/My.Plugin/Views/Shared/{0}.cshtml",
    };
}


// {0} in format gets filled with view name.
// {1} in format gets filled with controller name.

I am not sure which one will solve your problem.

Just replace My.Plugin with Misc.Rental an you should be fine.

Raphael
  • 990
  • 1
  • 13
  • 24
  • Thanks for your response. I resolved my issue. Issue lied in my Custom View registration within RouteProvider. \n I used **"Insert"** method instead of **"Add"** to register, it worked for me. `ViewEngines.Engines.Insert(0, new CustomViewEngine());` – Mukesh Jain Apr 11 '18 at 07:08
  • 1
    @Mukesh - if you still facing issue, than set order to -1 in RouteProvider. – Raju Paladiya Apr 25 '18 at 21:22