Skip to content

Instantly share code, notes, and snippets.

@miensol
Created August 27, 2013 15:33
Show Gist options
  • Save miensol/6355160 to your computer and use it in GitHub Desktop.
Save miensol/6355160 to your computer and use it in GitHub Desktop.
Loading views from outside of asp.net mvc root directory
using System.Collections.Specialized;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;
using System.Web.Routing;
namespace TestSamples.VirtualPath
{
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "TestWidget", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
HostingEnvironment.RegisterVirtualPathProvider(new WidgetVirtualPathProvider());
foreach (var engine in ViewEngines.Engines.Where(eng=> eng is WebFormViewEngine).Cast<WebFormViewEngine>())
{
engine.ViewLocationFormats = engine.ViewLocationFormats.Concat(new[]
{
"~/Widget/Views/{1}/{0}.aspx"
}).ToArray();
}
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
}
}
using System.IO;
using System.Web.Hosting;
namespace TestSamples.VirtualPath
{
public class WidgetVirtualFile : VirtualFile
{
public WidgetVirtualFile(string virtualPath) : base(virtualPath)
{
}
public override Stream Open()
{
FileStream fileStream = File.OpenRead(WidgetVirtualPathProvider.GetWidgetTargetpath(VirtualPath));
return fileStream;
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Web.Caching;
using System.Web.Hosting;
namespace TestSamples.VirtualPath
{
public class WidgetVirtualPathProvider : VirtualPathProvider
{
public const string BASE_WIDGET_DIR = "c:/Users/piotr_000/dev/c#/TestSamples/TestSamples.VirtualPath.Widget";
public override bool FileExists(string virtualPath)
{
if (IsWidgetPath(virtualPath))
{
var targetPath = GetWidgetTargetpath(virtualPath);
bool fileExists = File.Exists(targetPath);
return fileExists;
}
return base.FileExists(virtualPath);
}
private static bool IsWidgetPath(string virtualPath)
{
return string.IsNullOrWhiteSpace(virtualPath) == false && virtualPath.StartsWith("/Widget");
}
public static string GetWidgetTargetpath(string virtualPath)
{
var path = virtualPath.Replace("/Widget/", "").Replace("~","");
string targetPath = Path.Combine(BASE_WIDGET_DIR, path);
return targetPath;
}
public override VirtualFile GetFile(string virtualPath)
{
return new WidgetVirtualFile(virtualPath);
}
public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
{
if (virtualPathDependencies == null)
return (CacheDependency)null;
List<string> stringCollection = null;
foreach (string virtualPath1 in virtualPathDependencies)
{
if (IsWidgetPath(virtualPath))
{
string str = GetWidgetTargetpath(virtualPath1);
if (stringCollection == null)
stringCollection = new List<string>();
stringCollection.Add(str);
}
}
if (stringCollection == null)
return (CacheDependency) null;
return new CacheDependency(stringCollection.ToArray(), utcStart);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment