Skip to content

Instantly share code, notes, and snippets.

@smockle
Created March 7, 2014 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smockle/9403868 to your computer and use it in GitHub Desktop.
Save smockle/9403868 to your computer and use it in GitHub Desktop.
Allows multiple MVC sites to share the same Master Page, fetched from the server at compile time, instead of having to include it in each project.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web.Caching;
using System.Web.Hosting;
namespace CWSToolkit {
public enum CWSMasterPages { Default, OIT, Test, Bootstrap };
public class PathProvider : VirtualPathProvider {
public string remotePath = "https://cws.auburn.edu/shared/assets/masterpages/auburn.master.html";
/// <summary>
/// Intercepts requests for "site.master" and provides the default Auburn remote masterpage instead.
/// </summary>
public PathProvider(): base(){
}
/// <summary>
/// Intercepts requests for "site.master" and provides the specified remote masterpage instead.
/// </summary>
/// <param name="masterpage">The remote View Master Page (ASPX) to use. Specify from the CWSMasterPages enum (in the CWSToolkit namespace).</param>
public PathProvider(CWSMasterPages masterpage) {
if (masterpage == CWSMasterPages.OIT)
this.remotePath = "https://cws.auburn.edu/shared/assets/masterpages/oit.master.html";
else if (masterpage == CWSMasterPages.Test)
this.remotePath = "https://auburn.edu/oit/includes/test.master";
else if (masterpage == CWSMasterPages.Bootstrap)
this.remotePath = "https://cws.auburn.edu/shared/assets/masterpages/bootstrap.master.html";
else
this.remotePath = "https://cws.auburn.edu/shared/assets/masterpages/auburn.master.html";
}
/// <summary>
/// This usage is OBSOLETE. Please use the CWSMasterPages enum instead for the parameter.
/// </summary>
public PathProvider(string dud)
{
throw new System.ArgumentException("Specifying the shared masterpage in the global.asax no longer uses a string. Please use the CWSMasterPages enum type instead of a string. Ex: CWSMasterPages.OIT instead of \"oit\".");
}
public bool IsPathVirtual(string virtualPath) {
virtualPath = virtualPath.ToLower();
if (virtualPath.Contains("site.master"))
return true;
else if (virtualPath.Contains("shared-head.html"))
return true;
else
return false;
}
public override bool FileExists(string virtualPath) {
return IsPathVirtual(virtualPath) ? true : Previous.FileExists(virtualPath);
}
public override VirtualFile GetFile(string virtualPath) {
if (virtualPath.ToLower().Contains("site.master"))
return new RemoteFile(virtualPath, remotePath);
else if (virtualPath.ToLower().Contains("shared-head.html"))
return new RemoteFile(virtualPath, "http://auburn.edu/oit/includes/shared-head.html");
else
return Previous.GetFile(virtualPath);
}
public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) {
if (IsPathVirtual(virtualPath)) {
return null;
} else {
List<string> l = new List<string>();
foreach (var v in virtualPathDependencies) {
if (!(v.ToString().ToLower().Contains("includes")))
l.Add(v.ToString());
}
IEnumerable safeDependencies = l;
return Previous.GetCacheDependency(virtualPath, safeDependencies, utcStart);
}
}
}
public class RemoteFile : System.Web.Hosting.VirtualFile {
public string remotePath = "https://cws.auburn.edu/shared/assets/masterpages/auburn.master.html";
/// <summary>
/// Accept remote files in addition to standard files.
/// Specifically, use a remote View Master Page (ASPX).
/// </summary>
/// <param name="path">The default path.</param>
public RemoteFile(string path): base(path) {
}
/// <summary>
/// Accept remote files in addition to standard files.
/// Specifically, use a remote View Master Page (ASPX).
/// </summary>
/// <param name="path">The default path.</param>
/// <param name="remotePath">The remote View Master Page (ASPX) to use. Specify "default", "oit", or a custom url.</param>
public RemoteFile(string path, string remotePath): base(path) {
this.remotePath = remotePath;
}
public override System.IO.Stream Open() {
MemoryStream stream = new MemoryStream();
string path = remotePath;
HttpWebRequest request = WebRequest.Create(path) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) {
response.GetResponseStream().CopyTo(stream);
}
stream.Position = 0;
return stream;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment