How to load .cshtml file from plugin?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
I'm developing one plugin to store all keywords searched by user. For that I've to change in searchbox.cshml. I'm planning to copy SearchBox.cshtml into plugin.
But I don't know how to load .cshtml file from plugin?
11 years ago
Dharmik wrote:
I'm developing one plugin to store all keywords searched by user. For that I've to change in searchbox.cshml. I'm planning to copy SearchBox.cshtml into plugin.
But I don't know how to load .cshtml file from plugin?


You want to override the SearchBox.cshtml from your plugin?

Why not you create a Widget plugin, and replace the SearchBox.cshtml with your Widget plugin?

Anyway, since all you want to do is to store all keywords searched by users (and that's done at the programming part), why would you need to override SearchBox.cshtml? :)
11 years ago
actually , searchbox.cshtml call search method from catalog controller while submitting form.
I  want to call my plugin method first using inheritance. for that I've to declare plugin's controller method in searchbox.cshtml to submit form.
11 years ago
Dharmik wrote:
actually , searchbox.cshtml call search method from catalog controller while submitting form.
I  want to call my plugin method first using inheritance. for that I've to declare plugin's controller method in searchbox.cshtml to submit form.


You don't need that. You can use MVC Action Filters to do that. It's even easier and no interference of original nopCommerce files is needed. :)
11 years ago
wooncherk wrote:
actually , searchbox.cshtml call search method from catalog controller while submitting form.
I  want to call my plugin method first using inheritance. for that I've to declare plugin's controller method in searchbox.cshtml to submit form.

You don't need that. You can use MVC Action Filters to do that. It's even easier and no interference of original nopCommerce files is needed. :)


can u provide me small example for MVC Action Filters?
11 years ago
Dharmik wrote:
actually , searchbox.cshtml call search method from catalog controller while submitting form.
I  want to call my plugin method first using inheritance. for that I've to declare plugin's controller method in searchbox.cshtml to submit form.

You don't need that. You can use MVC Action Filters to do that. It's even easier and no interference of original nopCommerce files is needed. :)

can u provide me small example for MVC Action Filters?


MVC Action Filters are made up of 2 parts, the filter itself and a Filter Provider to register for your filter. The filter provider is optional and is one of the many ways of registering filters. But in our case, Filter Provider is a better solution since then we don't need to touch Global.asax.

An example of a Filter Provider is provided below:

using Nop.Admin.Controllers;
using Nop.Core.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Nop.Plugin.Misc.MyPlugin.Filters
{
    public class MyFilterProvider : IFilterProvider
    {
        private readonly IActionFilter _actionFilter;

        public MyFilterProvider(IActionFilter actionFilter)
        {
            _actionFilter = actionFilter;
        }

        public IEnumerable<Filter> GetFilters(ControllerContext controllerContext,
            ActionDescriptor actionDescriptor)
        {
            if (actionDescriptor.ControllerDescriptor.ControllerType == typeof(OrderController) &&
                actionDescriptor.ActionName.Equals("AddProductToOrderDetails") &&
                controllerContext.HttpContext.Request.HttpMethod == "POST")
            {
                return new Filter[]
                {
                    new Filter(_actionFilter, FilterScope.Action, null)
                };
            }

            return new Filter[] { };
        }
    }
}


The Action Filter itself is left for you to discover. Search over the internet for example.

After the filter and the filter provider are in place, you need to register them:

using Autofac;
using Autofac.Integration.Mvc;
using Nop.Core.Infrastructure;
using Nop.Core.Infrastructure.DependencyManagement;
using Nop.Plugin.Misc.AutoOrderTotal.Filters;
using System.Web.Mvc;

namespace Nop.Plugin.Misc.MyPlugin {
    public class DependencyRegistrar : IDependencyRegistrar
    {
        public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            builder.RegisterType<MyFilter>().As<IActionFilter>().InstancePerHttpRequest();
            builder.RegisterType<MyFilterProvider>().As<IFilterProvider>().InstancePerHttpRequest();
        }

        public int Order
        {
            get { return 0; }
        }
    }
}


Then magic happens. :)
11 years ago
wooncherk - in your DependencyRegistrar there is a method called Register that hooks up the ActionFilter.

Is this called automatically at some point? If not, then from where should this method be called? In the Install method of the plugin perhaps?
11 years ago
discopatrick wrote:
wooncherk - in your DependencyRegistrar there is a method called Register that hooks up the ActionFilter.

Is this called automatically at some point? If not, then from where should this method be called? In the Install method of the plugin perhaps?


IDependencyRegistrar.Register gets called automatically in Application_Start in Global.asax.cs. :D
11 years ago
Cool!

Ok, so now I'm wondering why my ActionFilter isn't firing.

I'm using the ActionFilter example at http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs which writes to the debug console when the events are fired:

public class LogActionFilter : ActionFilterAttribute

     {
          public override void OnActionExecuting(ActionExecutingContext filterContext)
          {
               Log("OnActionExecuting", filterContext.RouteData);      
          }

          public override void OnActionExecuted(ActionExecutedContext filterContext)
          {
               Log("OnActionExecuted", filterContext.RouteData);      
          }

          public override void OnResultExecuting(ResultExecutingContext filterContext)
          {
               Log("OnResultExecuting", filterContext.RouteData);      
          }

          public override void OnResultExecuted(ResultExecutedContext filterContext)
          {
               Log("OnResultExecuted", filterContext.RouteData);      
          }


          private void Log(string methodName, RouteData routeData)
          {
               var controllerName = routeData.Values["controller"];
               var actionName = routeData.Values["action"];
               var message = String.Format("{0} controller:{1} action:{2}", methodName, controllerName, actionName);
               Debug.WriteLine(message, "Action Filter Log");
          }

     }


And I wrote my FilterProvider to add the filter to the CustomerController:

if (actionDescriptor.ControllerDescriptor.ControllerType == typeof(CustomerController)


But if I put breakpoints in the ActionFilter code, or watch the debug console, I don't see anything happen.

I've cleaned/rebuilt the solution to make sure the files have been updated.

I should be able to debug my plugin even though it's copied to a directory in another project, shouldn't I?
11 years ago
discopatrick wrote:
Cool!

Ok, so now I'm wondering why my ActionFilter isn't firing.

I'm using the ActionFilter example at http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs which writes to the debug console when the events are fired:

public class LogActionFilter : ActionFilterAttribute

     {
          public override void OnActionExecuting(ActionExecutingContext filterContext)
          {
               Log("OnActionExecuting", filterContext.RouteData);      
          }

          public override void OnActionExecuted(ActionExecutedContext filterContext)
          {
               Log("OnActionExecuted", filterContext.RouteData);      
          }

          public override void OnResultExecuting(ResultExecutingContext filterContext)
          {
               Log("OnResultExecuting", filterContext.RouteData);      
          }

          public override void OnResultExecuted(ResultExecutedContext filterContext)
          {
               Log("OnResultExecuted", filterContext.RouteData);      
          }


          private void Log(string methodName, RouteData routeData)
          {
               var controllerName = routeData.Values["controller"];
               var actionName = routeData.Values["action"];
               var message = String.Format("{0} controller:{1} action:{2}", methodName, controllerName, actionName);
               Debug.WriteLine(message, "Action Filter Log");
          }

     }


And I wrote my FilterProvider to add the filter to the CustomerController:

if (actionDescriptor.ControllerDescriptor.ControllerType == typeof(CustomerController)


But if I put breakpoints in the ActionFilter code, or watch the debug console, I don't see anything happen.

I've cleaned/rebuilt the solution to make sure the files have been updated.

I should be able to debug my plugin even though it's copied to a directory in another project, shouldn't I?


Have you registered your filter and filterprovider in dependency registrar? Yes, you can debug the code. But make sure that during debugging, the breakpoint icon is a solid red dot. If it's a hollow red dot, then it means the debugger is not loaded correctly. :)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.