Maybe I am asking this wrong. Virtualpathprovider allows me to render a view as a string. The issue with this, is I want to render my layout page from a different project. This said view, has many different routes and scripts and css style, is in a live
version of the website and I would like to be able to show a preview without having to add the same routes and scripts and pretty much every file into my administration project.
Here is what I have so far, but I am lost as how to load my layout this way.
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Hosting;
namespace VirtualPathProviderExample.Core {
public class ExampleVirtualPathProvider : VirtualPathProvider {
private readonly string testAspxContent = "<%@ Page Language=\"C#\" MasterPageFile=\"~/Views/Shared/Site.Master\" Inherits=\"System.Web.Mvc.ViewPage\" %><asp:Content ID=\"indexTitle\" ContentPlaceHolderID=\"TitleContent\" runat=\"server\">Test Page</asp:Content><asp:Content ID=\"indexContent\" ContentPlaceHolderID=\"MainContent\" runat=\"server\"><h2><%= Html.Encode(ViewData[\"Message\"]) %></h2><br /><p>This is file by the name of <strong>~/Views/Home/Test.aspx</strong></p><p>This is a page that does not exist in anywhere but still it's there :D by means of VirtualPathProvider.</p></asp:Content>";
List<VirtualFile> virtualFiles = new List<VirtualFile>();
public ExampleVirtualPathProvider() {
virtualFiles.Add(new SimpleVirtualFile("~/Views/Home/Test.aspx", testAspxContent));
}
public override bool FileExists(string virtualPath) {
List<VirtualFile> files = (from f in virtualFiles where f.VirtualPath.Equals(virtualPath) select f).ToList();
if (files.Count > 0)
return true;
else
return base.FileExists(virtualPath);
}
private class SimpleVirtualFile : VirtualFile {
private string content;
public SimpleVirtualFile(string filename, string content)
: base(filename) {
this.content = content;
}
public override Stream Open() {
ASCIIEncoding encoding = new ASCIIEncoding();
return new MemoryStream(encoding.GetBytes(content), false);
}
}
public override VirtualFile GetFile(string virtualPath) {
List<VirtualFile> files = (from f in virtualFiles where f.VirtualPath.Equals(virtualPath) select f).ToList();
if (files.Count > 0) {
return files[0];
} else
return base.GetFile(virtualPath);
}
public override bool DirectoryExists(string virtualDir) {
return base.DirectoryExists(virtualDir);
}
public override VirtualDirectory GetDirectory(string virtualDir) {
return base.GetDirectory(virtualDir);
}
}
}
I guess, long story short, I should probably wire up the site to accept parameters on some authenticated section of the site.
I certain cannot do this because I get a virtualpath error.
Xequence
Contributor
4313 Points
1528 Posts
Re: mvc3 use external layout page
Apr 03, 2012 06:00 PM|LINK
Maybe I am asking this wrong. Virtualpathprovider allows me to render a view as a string. The issue with this, is I want to render my layout page from a different project. This said view, has many different routes and scripts and css style, is in a live version of the website and I would like to be able to show a preview without having to add the same routes and scripts and pretty much every file into my administration project.
Here is what I have so far, but I am lost as how to load my layout this way.
using System; using System.IO; using System.Text; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Hosting; namespace VirtualPathProviderExample.Core { public class ExampleVirtualPathProvider : VirtualPathProvider { private readonly string testAspxContent = "<%@ Page Language=\"C#\" MasterPageFile=\"~/Views/Shared/Site.Master\" Inherits=\"System.Web.Mvc.ViewPage\" %><asp:Content ID=\"indexTitle\" ContentPlaceHolderID=\"TitleContent\" runat=\"server\">Test Page</asp:Content><asp:Content ID=\"indexContent\" ContentPlaceHolderID=\"MainContent\" runat=\"server\"><h2><%= Html.Encode(ViewData[\"Message\"]) %></h2><br /><p>This is file by the name of <strong>~/Views/Home/Test.aspx</strong></p><p>This is a page that does not exist in anywhere but still it's there :D by means of VirtualPathProvider.</p></asp:Content>"; List<VirtualFile> virtualFiles = new List<VirtualFile>(); public ExampleVirtualPathProvider() { virtualFiles.Add(new SimpleVirtualFile("~/Views/Home/Test.aspx", testAspxContent)); } public override bool FileExists(string virtualPath) { List<VirtualFile> files = (from f in virtualFiles where f.VirtualPath.Equals(virtualPath) select f).ToList(); if (files.Count > 0) return true; else return base.FileExists(virtualPath); } private class SimpleVirtualFile : VirtualFile { private string content; public SimpleVirtualFile(string filename, string content) : base(filename) { this.content = content; } public override Stream Open() { ASCIIEncoding encoding = new ASCIIEncoding(); return new MemoryStream(encoding.GetBytes(content), false); } } public override VirtualFile GetFile(string virtualPath) { List<VirtualFile> files = (from f in virtualFiles where f.VirtualPath.Equals(virtualPath) select f).ToList(); if (files.Count > 0) { return files[0]; } else return base.GetFile(virtualPath); } public override bool DirectoryExists(string virtualDir) { return base.DirectoryExists(virtualDir); } public override VirtualDirectory GetDirectory(string virtualDir) { return base.GetDirectory(virtualDir); } } }I guess, long story short, I should probably wire up the site to accept parameters on some authenticated section of the site.
I certain cannot do this because I get a virtualpath error.
@model SF.Library.WebContent.ContentPageEdit @{ //ViewBag.Title = TempData["title"].ToString() + System.Configuration.ConfigurationManager.AppSettings["AppendAllPageTitles"].ToString(); Layout = "http://www.mysite.com/views/shared/_layout.cshtml"; } @{ ViewBag.Title = "Preview"; } <h2>Preview</h2> <div class="faq"> @{ var displayMessage = TempData["DisplayMessage"]; if (displayMessage != null) { if (displayMessage.ToString().ToLower().Contains("error")) { <div> <p> <font color="red">@displayMessage</font></p> </div> } else { <div> <p>@displayMessage</p> </div> } } } @Html.Raw(HttpUtility.HtmlDecode(@Model.ContentBody)) </div>Credentials