This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
/
LeBlenderController.cs
84 lines (70 loc) · 3.07 KB
/
LeBlenderController.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Umbraco.Web.Mvc;
using Lecoati.LeBlender.Extension.Models;
using Newtonsoft.Json;
using Umbraco.Web.Editors;
using Umbraco.Core.Logging;
namespace Lecoati.LeBlender.Extension.Controllers
{
public class LeBlenderController : SurfaceController
{
[ChildActionOnly]
public ActionResult RenderEditor(string editorAlias, string frontView, LeBlenderModel model)
{
// Check if the frontView is a custom path
if (string.IsNullOrEmpty(frontView))
{
frontView = String.Format("/views/partials/grid/editors/{0}.cshtml", Helper.FirstCharToUpper(editorAlias));
}
else if (frontView.IndexOf("/") < 0)
{
frontView = string.Format("/Views/Partials/Grid/Editors/{0}.cshtml", frontView);
}
// Look for a custom controller
var controllerType = Helper.GetLeBlenderController(editorAlias);
if (controllerType != null)
{
try
{
// Load a controller instance
var controllerInstance = (LeBlenderController)Activator.CreateInstance(controllerType);
controllerInstance.ControllerContext = this.ControllerContext;
// Take the view name as default method
var parts = frontView.Split(new char[] { '/', '\\' });
var method = parts.Last().Split('.').First();
var actionMethod = controllerType.GetMethod(method);
if (actionMethod == null)
{
method = "Index";
actionMethod = controllerType.GetMethod("Index");
}
if (actionMethod == null)
throw new Exception("No default method '" + method + "' was found");
// Set the specific model
var parameter = actionMethod.GetParameters().First();
var type = parameter.ParameterType.UnderlyingSystemType;
var typeInstance = (LeBlenderModel)Activator.CreateInstance(type);
typeInstance.Items = model.Items;
// Invoke the custom controller
var actionResult = (ViewResult)controllerType.GetMethod(method).Invoke(controllerInstance, new[] { typeInstance });
// Return the action result
if (string.IsNullOrWhiteSpace(actionResult.ViewName))
{
actionResult.ViewName = frontView;
}
return actionResult;
}
catch (Exception ex)
{
LogHelper.Error<LeBlenderController>("Could not load LeBlender invoke the custom controller", ex);
}
}
return PartialView(frontView, model);
}
}
}