using System.IO; using System.Net; using System.Web; using System.Web.Mvc; using Website.Filters; namespace Website.Controllers { [Compressable] [Cacheable(SupportedTypes = CacheType.ClientSide, ClientMaxAge = 4320)] // Client-side cache for 3 days public class StaticContentController : Controller { public ActionResult Scripts(string path) { return LocalContent("/content/js/" + path, "text/javascript"); } public ActionResult Styles(string path) { return LocalContent("/content/styles/" + path, "text/css"); } protected ActionResult LocalContent(string contentPath, string contentType) { var absolutePath = VirtualPathUtility.IsAbsolute(contentPath) ? contentPath : VirtualPathUtility.ToAbsolute(contentPath); var serverPath = Server.MapPath(absolutePath); // If we don't have a file at this location, send them to the 404 page! if (!File.Exists(serverPath)) return RedirectToAction("Index", "Error", new { statusCode = HttpStatusCode.NotFound.ToString() }); // If we've gotten here, we've verified the file, so go ahead and // open it up, and read it out to a Content Result. using (var contentStream = File.OpenRead(serverPath)) { return Content(new StreamReader(contentStream).ReadToEnd(), contentType); } } } }