View Engin Register issue in plugin.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
8 years ago
Hello,

I have created two plugins named for example named as CustomPlugin & MyPlugin. There is one common view page in both of these plugins but with different Model. I use View engine in both of these plugins. I have registered View Engine on 0 index in both plugins.

I want to know which plugin call that view page first? How View Engine works if it has two path for 0 index? Is there any way to make it first always?

Any help is appreciated.
8 years ago
I think View Engine is intialized in Dependency Register of your plugin. It is called by order of your dependency register.
8 years ago
This can be done by adding a new class:


using Nop.Web.Framework.Themes;

namespace Nop.Plugin.Misc.MyPlugin.ViewEngines
{
    class MyPluginViewEngine : ThemeableRazorViewEngine
    {
        public MyPluginViewEngine()
        {
            PartialViewLocationFormats =
                new[]
                {
                    "~/Plugins/Misc.MyPlugin/Views/{0}.cshtml"
                };

            ViewLocationFormats =
                new[]
                {
                    "~/Plugins/Misc.MyPlugin/Views/{0}.cshtml"
                };
        }
    }
}


and add this to top of your RouteProvider.cs - in public void RegisterRoutes(RouteCollection routes)

System.Web.Mvc.ViewEngines.Engines.Add(new MyPluginViewEngine());
8 years ago
Thank you Marc for your response.

I have done this already. My main problem is to have same view page in both plugin.

For example, view page name is xyz.cshtml. So my both plugin contain a separate view page in their own folder and with their model but view page name is same. Now when I run my application it gives me an error for model like expecting model is customplugin.model and it is getting myplugin.model.

How can I solve this?
8 years ago
Anki wrote:
Thank you Marc for your response.

I have done this already. My main problem is to have same view page in both plugin.

For example, view page name is xyz.cshtml. So my both plugin contain a separate view page in their own folder and with their model but view page name is same. Now when I run my application it gives me an error for model like expecting model is customplugin.model and it is getting myplugin.model.

How can I solve this?



on many plugin the view name is same just difference is plugin namespace and its output folder

new[]
                {
                    "~/Plugins/Misc.MyPlugin/Views/{0}.cshtml"
                };

so on your both plugin it should have different output folder name like "Misc.MyPlugin" and "Misc.MyCustomPlugin" etc

so if this both are good then i think its other issue.
8 years ago
I am getting this error,

The model item passed into the dictionary is of type 'Nop.Plugin.Models.CustomModel', but this dictionary requires a model item of type 'Nop.Plugin.Models.MyPluginModel'.

Because xyz.cshtml page is exist in both plugin. I am trying to run page of Custom plugin but it will find page in MyPlugin first. Hence it throws above error.
8 years ago
Anki wrote:
I am getting this error,

The model item passed into the dictionary is of type 'Nop.Plugin.Models.CustomModel', but this dictionary requires a model item of type 'Nop.Plugin.Models.MyPluginModel'.

Because xyz.cshtml page is exist in both plugin. I am trying to run page of Custom plugin but it will find page in MyPlugin first. Hence it throws above error.


from controller action you are returning Nop.Plugin.Models.CustomModel and in xyz.cshtml you have used
Nop.Plugin.Models.MyPluginModel so as both model is not matching it throw this exception.

so its not issue of not finding view but its issue of you are not using same model on both controller action and view it self
8 years ago
Anki wrote:
I am getting this error,

The model item passed into the dictionary is of type 'Nop.Plugin.Models.CustomModel', but this dictionary requires a model item of type 'Nop.Plugin.Models.MyPluginModel'.

Because xyz.cshtml page is exist in both plugin. I am trying to run page of Custom plugin but it will find page in MyPlugin first. Hence it throws above error.


public void RegisterRoutes(RouteCollection routes)
        {
            System.Web.Mvc.ViewEngines.Engines.Insert(0, new CustomViewEngine());
        }

        public int Priority
        {
            get { return 10; }
        }

You register your route in routeprovider. Increase your priority to get desired result.
8 years ago
Let me explain you it again,

I have created two plugins. 1. CustomPlugin 2. MyPlugin

I use _productBox.cshtml view page in both plugin.

Now, I have used View Engine in both plugin like:

-->For CustomPlugin
PartialViewLocationFormats =
                new[]
                {      //themes
                      "~/Themes/{2}/Views/{1}/{0}.cshtml",                  
                     "~/Plugins/CustomPlugin/Views/{1}/{0}.cshtml"
                };

--> MyPlugin
PartialViewLocationFormats =
                new[]
                {      //themes
                      "~/Themes/{2}/Views/{1}/{0}.cshtml",                  
                     "~/Plugins/MyPlugin/Views/{1}/{0}.cshtml"
                };

Then I register view engine from Route file like:
ViewEngines.Engines.Insert(0, new CustomPluginViewEngine());
and
ViewEngines.Engines.Insert(0, new MyPluginViewEngine());
respectively.

Now, both of this plugin has _productBox.cshtml page with their own model.

Both plugin's functionality won't work together for this page, if both are installed. If only one is installed then it works fine.

Hope this will helps you to understand my problem clearly.
8 years ago
Only one will work at a time. Both will not work using view engine.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.