-
using System;
-
using System.Globalization;
-
using System.Web.Mvc;
-
using System.Linq;
-
-
namespace Mvc1Areas
-
{
-
public sealed class AreaAwareViewEngine : VirtualPathProviderViewEngine
-
{
-
private const string _cacheKeyFormat = ":ViewCacheEntry:{0}:{1}:{2}:{3}:{4}:";
-
private const string _cacheKeyPrefix_Master = "Master";
-
private const string _cacheKeyPrefix_Partial = "Partial";
-
private const string _cacheKeyPrefix_View = "View";
-
private static readonly string[] _emptyLocations = new string[0];
-
-
public AreaAwareViewEngine()
-
{
-
MasterLocationFormats = new string[] {
-
"~/Areas/{2}/Views/{1}/{0}.master",
-
"~/Views/{1}/{0}.master",
-
"~/Views/Shared/{0}.master"
-
};
-
-
ViewLocationFormats = new string[] {
-
"~/Areas/{2}/Views/{1}/{0}.aspx",
-
"~/Views/{1}/{0}.aspx",
-
"~/Views/Shared/{0}.aspx"
-
};
-
-
PartialViewLocationFormats = ViewLocationFormats;
-
}
-
-
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
-
{
-
return new WebFormView(partialPath, null);
-
}
-
-
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
-
{
-
return new WebFormView(viewPath, masterPath);
-
}
-
-
-
private string CreateCacheKey(string prefix, string name, string controllerName, string area)
-
{
-
return string.Format(CultureInfo.InvariantCulture, _cacheKeyFormat, new object[] { base.GetType().AssemblyQualifiedName, prefix, name, controllerName, area });
-
}
-
-
-
public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
-
{
-
string[] strArray;
-
if (controllerContext == null) {
-
throw new ArgumentNullException("controllerContext");
-
}
-
if (string.IsNullOrEmpty(partialViewName)) {
-
throw new ArgumentException("Value cannot be null or empty.", "partialViewName");
-
}
-
-
string requiredString = controllerContext.RouteData.GetRequiredString("controller");
-
object area;
-
controllerContext.RouteData.Values.TryGetValue("area", out area);
-
-
string str2 = this.GetPath(controllerContext, this.PartialViewLocationFormats, "PartialViewLocationFormats", partialViewName, requiredString, (string)area, "Partial", useCache, out strArray);
-
if (string.IsNullOrEmpty(str2)) {
-
return new ViewEngineResult(strArray);
-
}
-
return new ViewEngineResult(this.CreatePartialView(controllerContext, str2), this);
-
}
-
-
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
-
{
-
string[] strArray;
-
string[] strArray2;
-
if (controllerContext == null) {
-
throw new ArgumentNullException("controllerContext");
-
}
-
if (string.IsNullOrEmpty(viewName)) {
-
throw new ArgumentException("Value cannot be null or empty.", "viewName");
-
}
-
-
string requiredString = controllerContext.RouteData.GetRequiredString("controller");
-
object area;
-
controllerContext.RouteData.Values.TryGetValue("area", out area);
-
-
string str2 = this.GetPath(controllerContext, this.ViewLocationFormats, "ViewLocationFormats", viewName, requiredString, (string)area, "View", useCache, out strArray);
-
string str3 = this.GetPath(controllerContext, this.MasterLocationFormats, "MasterLocationFormats", masterName, requiredString, (string)area, "Master", useCache, out strArray2);
-
if (!string.IsNullOrEmpty(str2) && (!string.IsNullOrEmpty(str3) || string.IsNullOrEmpty(masterName))) {
-
return new ViewEngineResult(this.CreateView(controllerContext, str2, str3), this);
-
}
-
return new ViewEngineResult(strArray.Union<string>(strArray2));
-
}
-
-
private string GetPath(ControllerContext controllerContext, string[] locations, string locationsPropertyName, string name, string controllerName, string areaName, string cacheKeyPrefix, bool useCache, out string[] searchedLocations)
-
{
-
searchedLocations = _emptyLocations;
-
if (string.IsNullOrEmpty(name)) {
-
return string.Empty;
-
}
-
if ((locations == null) || (locations.Length == 0)) {
-
throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, "The property '{0}' cannot be null or empty.", new object[] { locationsPropertyName }));
-
}
-
bool flag = IsSpecificPath(name);
-
string key = this.CreateCacheKey(cacheKeyPrefix, name, flag ? string.Empty : controllerName, flag ? string.Empty : areaName);
-
if (useCache) {
-
string viewLocation = this.ViewLocationCache.GetViewLocation(controllerContext.HttpContext, key);
-
if (viewLocation != null) {
-
return viewLocation;
-
}
-
}
-
if (!flag) {
-
return this.GetPathFromGeneralName(controllerContext, locations, name, controllerName, areaName, key, ref searchedLocations);
-
}
-
return this.GetPathFromSpecificName(controllerContext, name, key, ref searchedLocations);
-
}
-
-
private string GetPathFromGeneralName(ControllerContext controllerContext, string[] locations, string name, string controllerName, string areaName, string cacheKey, ref string[] searchedLocations)
-
{
-
string virtualPath = string.Empty;
-
searchedLocations = new string[locations.Length];
-
for (int i = 0; i < locations.Length; i++) {
-
if (string.IsNullOrEmpty(areaName) && locations[i].Contains("{2}")) {
-
continue;
-
}
-
-
string str2 = string.Format(CultureInfo.InvariantCulture, locations[i], new object[] { name, controllerName, areaName });
-
if (this.FileExists(controllerContext, str2)) {
-
searchedLocations = _emptyLocations;
-
virtualPath = str2;
-
this.ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, virtualPath);
-
return virtualPath;
-
}
-
searchedLocations[i] = str2;
-
}
-
return virtualPath;
-
}
-
-
private string GetPathFromSpecificName(ControllerContext controllerContext, string name, string cacheKey, ref string[] searchedLocations)
-
{
-
string virtualPath = name;
-
if (!this.FileExists(controllerContext, name)) {
-
virtualPath = string.Empty;
-
searchedLocations = new string[] { name };
-
}
-
this.ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, cacheKey, virtualPath);
-
return virtualPath;
-
}
-
-
private static bool IsSpecificPath(string name)
-
{
-
char ch = name[0];
-
if (ch != '~') {
-
return (ch == '/');
-
}
-
return true;
-
}
-
}
-
}