nopcommerce里面的@Html.Widget("home_page_top") 是什么?

很多朋友在修改模板的时候看到很多类似@Html.Widget("xxx")的东西,这里简单介绍一下流程:

比如@Html.Widget("home_page_top"),首先要知道Html.Widget是什么,这是Html的一个扩展方法,位于Nop.Web.Framework\HtmlExtensions.cs

1
2
3
4
public  static  MvcHtmlString Widget( this  HtmlHelper helper,  string  widgetZone)
  {
        return  helper.Action( "WidgetsByZone" "Widget" new  { widgetZone = widgetZone });
  }


可以看到这里面调用的是action,找到WidgetController下面的WidgetsByZone,这是一个child action(不懂的百度一下),读一下代码,就能了解这个方法就是通过反射获取到实现接口IWidgetPlugin并且GetWidgetZones()包含home_page_top的插件的列表,然后创建一个model传递给试图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[ChildActionOnly]
         public  ActionResult WidgetsByZone( string  widgetZone)
         {
             //model
             var  model =  new  List<RenderWidgetModel>();
 
             var  widgets = _widgetService.LoadActiveWidgetsByWidgetZone(widgetZone, _storeContext.CurrentStore.Id);
             foreach  ( var  widget  in  widgets)
             {
                 var  widgetModel =  new  RenderWidgetModel();
 
                 string  actionName;
                 string  controllerName;
                 RouteValueDictionary routeValues;
                 widget.GetDisplayWidgetRoute(widgetZone,  out  actionName,  out controllerName,  out  routeValues);
                 widgetModel.ActionName = actionName;
                 widgetModel.ControllerName = controllerName;
                 widgetModel.RouteValues = routeValues;
 
                 model.Add(widgetModel);
             }
 
             return  PartialView(model);
         }

 

打开试图Widget\WidgetsByZone.cshtml:

1
2
3
4
5
6
@model List<RenderWidgetModel>
@ using  Nop.Web.Models.Cms;
@ foreach  ( var  widget  in  Model)
{
     @Html.Action(widget.ActionName, widget.ControllerName, widget.RouteValues)
}


这个试图的目的就是循环输出html,具体输出的内容在插件里面实现的,比如插件Nop.Plugin.Widgets.NivoSlider里面有个NivoSliderPlugin,这类插件必须继承自BasePlugin,和IWidgetPlugin,里面的方法GetDisplayWidgetRoute就是用于返回显示这个插件内容的action的信息,WidgetsNivoSliderController.cs里面的public ActionResult PublicInfo(string widgetZone)就是这个插件具体输出的内容,大体流程就是这样了。

原文出处 http://www.nopchina.net/post/nopcommerce-widget.html

转载于:https://www.cnblogs.com/tongdengquan/p/6090461.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值