/
RenderMvcController.cs
115 lines (102 loc) · 3.97 KB
/
RenderMvcController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Web.Mvc;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Web.Models;
using Umbraco.Web.Routing;
namespace Umbraco.Web.Mvc
{
/// <summary>
/// The default controller to render front-end requests
/// </summary>
[PreRenderViewActionFilter]
public class RenderMvcController : UmbracoController, IRenderMvcController
{
public RenderMvcController()
: base()
{
ActionInvoker = new RenderActionInvoker();
}
public RenderMvcController(UmbracoContext umbracoContext, UmbracoHelper umbracoHelper)
: base(umbracoContext, umbracoHelper)
{
}
public RenderMvcController(UmbracoContext umbracoContext)
: base(umbracoContext)
{
}
private PublishedContentRequest _publishedContentRequest;
/// <summary>
/// Returns the current UmbracoContext
/// </summary>
public override UmbracoContext UmbracoContext
{
get { return PublishedContentRequest.RoutingContext.UmbracoContext; }
}
/// <summary>
/// Returns the Current published content item for rendering the content
/// </summary>
protected IPublishedContent CurrentPage
{
get { return PublishedContentRequest.PublishedContent; }
}
/// <summary>
/// Returns the current PublishedContentRequest
/// </summary>
protected internal virtual PublishedContentRequest PublishedContentRequest
{
get
{
if (_publishedContentRequest != null)
return _publishedContentRequest;
if (RouteData.DataTokens.ContainsKey("umbraco-doc-request") == false)
{
throw new InvalidOperationException("DataTokens must contain an 'umbraco-doc-request' key with a PublishedContentRequest object");
}
_publishedContentRequest = (PublishedContentRequest)RouteData.DataTokens["umbraco-doc-request"];
return _publishedContentRequest;
}
}
/// <summary>
/// Checks to make sure the physical view file exists on disk
/// </summary>
/// <param name="template"></param>
/// <returns></returns>
protected bool EnsurePhsyicalViewExists(string template)
{
var result = ViewEngines.Engines.FindView(ControllerContext, template, null);
if (result.View == null)
{
LogHelper.Warn<RenderMvcController>("No physical template file was found for template " + template);
return false;
}
return true;
}
/// <summary>
/// Returns an ActionResult based on the template name found in the route values and the given model.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
/// <returns></returns>
/// <remarks>
/// If the template found in the route values doesn't physically exist, then an empty ContentResult will be returned.
/// </remarks>
protected ActionResult CurrentTemplate<T>(T model)
{
var template = ControllerContext.RouteData.Values["action"].ToString();
if (EnsurePhsyicalViewExists(template) == false)
throw new Exception("No physical template file was found for template " + template);
return View(template, model);
}
/// <summary>
/// The default action to render the front-end view
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[RenderIndexActionSelector]
public virtual ActionResult Index(RenderModel model)
{
return CurrentTemplate(model);
}
}
}