今天具体研究一下页面是如何渲染的。从首页看开始,拿\Presentation\Nop.Web\Views\Home\Index.cshtml中的@Html.Widget("home_page_top")这句来学习。
它对应的后台代码是这句:
路径:\Presentation\Nop.Web.Framework\HtmlExtensions.cs
public static MvcHtmlString Widget(this HtmlHelper helper, string widgetZone, object additionalData = null)
{
return helper.Action("WidgetsByZone", "Widget", new { widgetZone = widgetZone, additionalData = additionalData });
}
以上代码会调用:
路径:\Presentation\Nop.Web\Controllers\WidgetController.cs
[ChildActionOnly]
public ActionResult WidgetsByZone(string widgetZone, object additionalData = null)
{
var cacheKey = string.Format(ModelCacheEventConsumer.WIDGET_MODEL_KEY, _storeContext.CurrentStore.Id, widgetZone);
var cacheModel = _cacheManager.Get(cacheKey, () =>
{
//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 model;
});
//no data?
if (cacheModel.Count == 0)
return Content("");
//"RouteValues