首页 > 其他分享 >NopCommerce的页面展示

NopCommerce的页面展示

时间:2022-11-23 08:22:06浏览次数:44  
标签:展示 widgetModel var Html widgetZone NopCommerce Action new 页面

MVC架构,首先”急功近利“选择V下手,在路由中可以看到路由配置中的View,找到首页View

Nop.Web\Views\Home\Index.cshtml

  1. @{
  2. Layout = "~/Views/Shared/_ColumnsThree.cshtml";
  3. }
  4. <div class="page home-page">
  5. <div class="page-body">
  6. @Html.Widget("home_page_top")
  7. @Html.Action("TopicBlock", "Topic", new { systemName = "HomePageText" })
  8. @Html.Action("HomepageCategories", "Catalog")
  9. @Html.Action("HomepageProducts", "Product")
  10. @Html.Action("HomepageBestSellers", "Product")
  11. @Html.Action("HomePageNews", "News")
  12. @Html.Action("HomePagePolls", "Poll")
  13. @Html.Widget("home_page_bottom")
  14. </div>
  15. </div>

页面分布局及内容分块,在asp.net MVC中常用的方法是share文件夹中的布局视图

  1. Layout = "~/Views/Shared/_ColumnsThree.cshtml";

分块中常采取的办法有Partial视图或是Action,如上的Html.Action("HomepageCategories", "Catalog")

NopCommerce中还实现了Widget部件的方法

它其实是采用HtmlHelper的扩展方法:

 public static mvcHtmlString Widget(this HtmlHelper helper, string widgetZone, object additionalData = null)

  1. {
  2. return helper.Action("WidgetsByZone", "Widget", new { widgetZone = widgetZone, additionalData = additionalData });
  3. }

Widget部件实质又使用Action实现

扩展方法调用的是WidgetController中的WidgetByZone方法,最终返回的一个类型为MvcHtmlString字符串。

WidgetsByZone方法:

 

  1. [ChildActionOnly]
  2. public ActionResult WidgetsByZone(string widgetZone, object additionalData = null)
  3. {
  4. var cacheKey = string.Format(ModelCacheEventConsumer.WIDGET_MODEL_KEY, _storeContext.CurrentStore.Id, widgetZone);
  5. var cacheModel = _cacheManager.Get(cacheKey, () =>
  6. {
  7. //获取部件的Model集合
  8. var model = new List<RenderWidgetModel>();
  9. var widgets = _widgetService.LoadActiveWidgetsByWidgetZone(widgetZone, _storeContext.CurrentStore.Id);
  10. foreach (var widget in widgets)
  11. {
  12. var widgetModel = new RenderWidgetModel();
  13. string actionName;
  14. string controllerName;
  15. RouteValueDictionary routeValues;
  16. widget.GetDisplayWidgetRoute(widgetZone, out actionName, out controllerName, out routeValues);
  17. widgetModel.ActionName = actionName;
  18. widgetModel.ControllerName = controllerName;
  19. widgetModel.RouteValues = routeValues;
  20. model.Add(widgetModel);
  21. }
  22. return model;
  23. });
  24. //no data?
  25. if (cacheModel.Count == 0)
  26. return Content("");
  27. //"RouteValues" property of widget models depends on "additionalData".
  28. //We need to clone the cached model before modifications (the updated one should not be cached)
  29. var clonedModel = new List<RenderWidgetModel>();
  30. foreach (var widgetModel in cacheModel)
  31. {
  32. var clonedWidgetModel = new RenderWidgetModel();
  33. clonedWidgetModel.ActionName = widgetModel.ActionName;
  34. clonedWidgetModel.ControllerName = widgetModel.ControllerName;
  35. if (widgetModel.RouteValues != null)
  36. clonedWidgetModel.RouteValues = new RouteValueDictionary(widgetModel.RouteValues);
  37. if (additionalData != null)
  38. {
  39. if (clonedWidgetModel.RouteValues == null)
  40. clonedWidgetModel.RouteValues = new RouteValueDictionary();
  41. clonedWidgetModel.RouteValues.Add("additionalData", additionalData);
  42. }
  43. clonedModel.Add(clonedWidgetModel);
  44. }
  45. return PartialView(clonedModel);
  46. }

这个Action对应的视图Nop.Web\Views\Widget\WidgetsByZone.cshtml

 @model List<RenderWidgetModel>

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

可以看出,部件可以再包含部件或部件列

部件本身也是MVC模式,其M的实现为WidgetService

在WidgetController中的Action方法WidgetsByZone中我们可以看到里面最终调用WidgetService的方法LoadActiveWidgetsByWidgetZone并把结果缓存起来,而LoadActiveWidgetsByWidgetZone返回是一个类型IWidgetPlugin部件插件集合IList<IWidgetPlugin>。

 public virtual IList<IWidgetPlugin> LoadActiveWidgetsByWidgetZone(string widgetZone, int storeId = 0)

  1. {
  2. if (String.IsNullOrWhiteSpace(widgetZone))
  3. return new List<IWidgetPlugin>();
  4. return LoadActiveWidgets(storeId)
  5. .Where(x => x.GetWidgetZones().Contains(widgetZone, StringComparer.InvariantCultureIgnoreCase))
  6. .ToList();
  7. }
  1. public virtual IList<IWidgetPlugin> LoadAllWidgets(int storeId = 0)
  2. {
  3. return _pluginFinder.GetPlugins<IWidgetPlugin>(storeId: storeId).ToList();

 

标签:展示,widgetModel,var,Html,widgetZone,NopCommerce,Action,new,页面
From: https://www.cnblogs.com/5x19/p/16917114.html

相关文章