/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Globalization;
namespace Everest.Library.Mvc.View{
public class EverestWebFormViewEngine : System.Web.Mvc.WebFormViewEngine
{
public EverestWebFormViewEngine()
{
base.ViewLocationFormats = new string[] {
"{2}Views/{1}/{0}.aspx",
"{2}Views/{1}/{0}.ascx",
"{2}Views/Shared/{0}.aspx",
"{2}Views/Shared/{0}.ascx"
};
base.MasterLocationFormats = new string[] {
"{2}Views/{1}/{0}.master",
"{2}Views/Shared/{0}.master"
};
base.PartialViewLocationFormats = new string[] {
"{2}Views/{1}/{0}.aspx",
"{2}Views/{1}/{0}.ascx",
"{2}Views/Shared/{0}.aspx",
"{2}Views/Shared/{0}.ascx"
};
}
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
if (string.IsNullOrEmpty(viewName))
{
throw new ArgumentException("Value is required.", "viewName");
}
string[] searchedViewLocations;
string[] searchedMasterLocations;
string controllerName = controllerContext.RouteData.GetRequiredString("controller");
string viewPath = this.GetPath(controllerContext,this.ViewLocationFormats, viewName, controllerName, out searchedViewLocations);
string masterPath = this.GetPath(controllerContext,this.MasterLocationFormats, viewName, controllerName, out searchedMasterLocations);
if (!(string.IsNullOrEmpty(viewPath)) && (!(masterPath == string.Empty) || string.IsNullOrEmpty(masterName)))
{
return new ViewEngineResult(this.CreateView(controllerContext, viewPath, masterPath), this);
}
return new ViewEngineResult(searchedViewLocations.Union<string>(searchedMasterLocations));
}
public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
}
if (string.IsNullOrEmpty(partialViewName))
{
throw new ArgumentException("Value is required.", partialViewName);
}
string[] searchedLocations;
string controllerName = controllerContext.RouteData.GetRequiredString("controller");
string partialPath = this.GetPath(controllerContext, this.PartialViewLocationFormats, partialViewName, controllerName, out searchedLocations);
if (string.IsNullOrEmpty(partialPath))
{
return new ViewEngineResult(searchedLocations);
}
return new ViewEngineResult(this.CreatePartialView(controllerContext, partialPath), this);
}
protected virtual string GetPath(ControllerContext controllerContext, string[] locations, string viewName, string controllerName, out string[] searchedLocations)
{
string path = null;
searchedLocations = new string[locations.Length];
EverestContext everestContext = EverestContext.GetContext();
string rootUrl = everestContext.BaseUrl;
for (int i = 0; i < locations.Length; i++)
{
path = string.Format(CultureInfo.InvariantCulture, locations[i], new object[] { viewName, controllerName, rootUrl });
if (this.VirtualPathProvider.FileExists(path))
{
searchedLocations = new string[0];
return path;
}
searchedLocations[i] = path;
}
return null;
}
}
}
|