1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Web;
|
---|
5 | using System.Web.Mvc;
|
---|
6 | using System.Diagnostics;
|
---|
7 |
|
---|
8 | namespace HLWebPluginHost.PluginLib {
|
---|
9 |
|
---|
10 | /// <summary>
|
---|
11 | /// This implementation of the view engine takes view inside of plugins into account and removes the need to provide the full
|
---|
12 | /// path of a view from within a controller inside a view.
|
---|
13 | /// Use Global.asax to register this engine to the framework
|
---|
14 | /// </summary>
|
---|
15 | public class PluginViewEngine : System.Web.Mvc.WebFormViewEngine {
|
---|
16 | public PluginViewEngine(string[] viewLocations)
|
---|
17 | : base() {
|
---|
18 | string[] tempArray = new string[ViewLocationFormats.Length + viewLocations.Length];
|
---|
19 | ViewLocationFormats.CopyTo(tempArray, 0);
|
---|
20 |
|
---|
21 | for (int i = 0; i < viewLocations.Length; i++) {
|
---|
22 |
|
---|
23 | tempArray[ViewLocationFormats.Length + i] = viewLocations[i];
|
---|
24 | }
|
---|
25 |
|
---|
26 | ViewLocationFormats = tempArray;
|
---|
27 |
|
---|
28 | PartialViewLocationFormats = ViewLocationFormats;
|
---|
29 | }
|
---|
30 |
|
---|
31 | private bool IsAppResourcePath(string virtualPath) {
|
---|
32 |
|
---|
33 | String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
|
---|
34 | return checkPath.StartsWith("~/Plugins/", StringComparison.InvariantCultureIgnoreCase);
|
---|
35 | }
|
---|
36 |
|
---|
37 | //If we have a virtual path, we need to override the super class behavior,
|
---|
38 | //its implementation ignores custom VirtualPathProviders, unlike the super's super class.
|
---|
39 | //This code basically just reimplements the super-super class (VirtualPathProviderViewEngine) behavior for virtual paths.
|
---|
40 | protected override bool FileExists(ControllerContext controllerContext, string virtualPath) {
|
---|
41 |
|
---|
42 | if (IsAppResourcePath(virtualPath)) {
|
---|
43 |
|
---|
44 | return System.Web.Hosting.HostingEnvironment.VirtualPathProvider.FileExists(virtualPath);
|
---|
45 | } else
|
---|
46 | return base.FileExists(controllerContext, virtualPath);
|
---|
47 | }
|
---|
48 | }
|
---|
49 | } |
---|