Go to the documentation of this file.00001 using System;
00002 using System.Web.Mvc;
00003 using System.Web.Mvc.Html;
00004 using System.Web.Routing;
00005 using N2.Engine;
00006 using System.Diagnostics;
00007
00008 namespace N2.Web.Mvc.Html
00009 {
00010 public interface ITemplateRenderer
00011 {
00012 void RenderTemplate(ContentItem item, HtmlHelper helper);
00013 }
00014
00015 [Service(typeof(ITemplateRenderer))]
00016 public class TemplateRenderer : ITemplateRenderer
00017 {
00018 private readonly IControllerMapper controllerMapper;
00019
00020 public TemplateRenderer(IControllerMapper controllerMapper)
00021 {
00022 this.controllerMapper = controllerMapper;
00023 }
00024
00025
00026 public void RenderTemplate(ContentItem item, HtmlHelper helper)
00027 {
00028 Type itemType = item.GetContentType();
00029 string controllerName = controllerMapper.GetControllerName(itemType);
00030 if(string.IsNullOrEmpty(controllerName))
00031 {
00032 Trace.TraceWarning("Found no controller for type " + itemType);
00033 return;
00034 }
00035
00036 RouteValueDictionary values = GetRouteValues(helper, item, controllerName);
00037
00038 helper.RenderAction("Index", values);
00039 }
00040
00041 private static RouteValueDictionary GetRouteValues(HtmlHelper helper, ContentItem item, string controllerName)
00042 {
00043 var values = new RouteValueDictionary();
00044 values[ContentRoute.ControllerKey] = controllerName;
00045 values[ContentRoute.ActionKey] = "Index";
00046 values[ContentRoute.ContentItemKey] = item.ID;
00047
00048
00049 var vpd = helper.RouteCollection.GetVirtualPath(helper.ViewContext.RequestContext, values);
00050 if (vpd == null)
00051 throw new InvalidOperationException("Unable to render " + item + " (" + values.ToQueryString() + " did not match any route)");
00052
00053 values["area"] = vpd.DataTokens["area"];
00054 return values;
00055 }
00056 }
00057 }