mvc3 use external layout pagehttp://forums.asp.net/t/1788756.aspx/1?mvc3+use+external+layout+pageTue, 10 Apr 2012 13:50:44 -040017887564914467http://forums.asp.net/p/1788756/4914467.aspx/1?mvc3+use+external+layout+pagemvc3 use external layout page <p>I am attempting to build a preview functionality that allows the user to see the page as if it were in the live site. The thing I want to do is load that external layout file without having to copy the views or setup the global asax in the local project.</p> 2012-04-03T16:43:41-04:004914595http://forums.asp.net/p/1788756/4914595.aspx/1?Re+mvc3+use+external+layout+pageRe: mvc3 use external layout page <p>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.</p> <p>&nbsp;</p> <p>Here is what I have so far, but I am lost as how to load my layout this way.</p> <pre class="prettyprint">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 = &quot;&lt;%@ Page Language=\&quot;C#\&quot; MasterPageFile=\&quot;~/Views/Shared/Site.Master\&quot; Inherits=\&quot;System.Web.Mvc.ViewPage\&quot; %&gt;&lt;asp:Content ID=\&quot;indexTitle\&quot; ContentPlaceHolderID=\&quot;TitleContent\&quot; runat=\&quot;server\&quot;&gt;Test Page&lt;/asp:Content&gt;&lt;asp:Content ID=\&quot;indexContent\&quot; ContentPlaceHolderID=\&quot;MainContent\&quot; runat=\&quot;server\&quot;&gt;&lt;h2&gt;&lt;%= Html.Encode(ViewData[\&quot;Message\&quot;]) %&gt;&lt;/h2&gt;&lt;br /&gt;&lt;p&gt;This is file by the name of &lt;strong&gt;~/Views/Home/Test.aspx&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;This is a page that does not exist in anywhere but still it's there :D by means of VirtualPathProvider.&lt;/p&gt;&lt;/asp:Content&gt;&quot;; List&lt;VirtualFile&gt; virtualFiles = new List&lt;VirtualFile&gt;(); public ExampleVirtualPathProvider() { virtualFiles.Add(new SimpleVirtualFile(&quot;~/Views/Home/Test.aspx&quot;, testAspxContent)); } public override bool FileExists(string virtualPath) { List&lt;VirtualFile&gt; files = (from f in virtualFiles where f.VirtualPath.Equals(virtualPath) select f).ToList(); if (files.Count &gt; 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&lt;VirtualFile&gt; files = (from f in virtualFiles where f.VirtualPath.Equals(virtualPath) select f).ToList(); if (files.Count &gt; 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); } } }</pre> <p>&nbsp;</p> <p>I guess, long story short, I should probably wire up the site to accept parameters on some authenticated section of the site.</p> <p>&nbsp;</p> <p>I certain cannot do this because I get a virtualpath error.</p> <pre class="prettyprint">@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"; } &lt;h2&gt;Preview&lt;/h2&gt; &lt;div class="faq"&gt; @{ var displayMessage = TempData["DisplayMessage"]; if (displayMessage != null) { if (displayMessage.ToString().ToLower().Contains("error")) { &lt;div&gt; &lt;p&gt; &lt;font color="red"&gt;@displayMessage&lt;/font&gt;&lt;/p&gt; &lt;/div&gt; } else { &lt;div&gt; &lt;p&gt;@displayMessage&lt;/p&gt; &lt;/div&gt; } } } @Html.Raw(HttpUtility.HtmlDecode(@Model.ContentBody)) &lt;/div&gt;</pre> <p></p> <p>&nbsp;</p> 2012-04-03T18:00:47-04:004914603http://forums.asp.net/p/1788756/4914603.aspx/1?Re+mvc3+use+external+layout+pageRe: mvc3 use external layout page <p>my error:</p> <h1>Server Error in '/' Application.</h1> <hr color="silver" size="1" width="100%"> <h2><i>'http:/www.xxx.com/views/shared/xxx/_layout.cshtml' is not a valid virtual path.</i></h2> <p><span face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif " style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif"><span face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif " style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif"><b>Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. <br> <b>Exception Details: </b>System.Web.HttpException: 'http:/www.xxx.com/views/shared/xxx/_layout.cshtml' is not a valid virtual path.<br> <b>Source Error:</b></span></span></p> <table width="100%" bgcolor="#ffffcc"> <tbody> <tr> <td><pre class="prettyprint">An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</pre></td> </tr> </tbody> </table> <p><span style="font-family: Arial, Helvetica, Geneva, SunSans-Regular, sans-serif;" face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "><span style="font-family: Arial, Helvetica, Geneva, SunSans-Regular, sans-serif;" face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "><b>Stack Trace:</b></span></span></p> <table width="100%" bgcolor="#ffffcc"> <tbody> <tr> <td> <pre>[HttpException (0x80004005): 'http:/www.xxxx.com/views/shared/xxx/_layout.cshtml' is not a valid virtual path.] System.Web.Util.UrlPath.CheckValidVirtualPath(String path) +332 System.Web.Util.UrlPath.Combine(String appPath, String basepath, String relative) +304 System.Web.Util.UrlPath.Combine(String basepath, String relative) +48 System.Web.VirtualPath.Combine(VirtualPath relativePath) +178 System.Web.VirtualPath.Combine(VirtualPath v1, VirtualPath v2) +108 System.Web.VirtualPathUtility.Combine(String basePath, String relativePath) +64 System.Web.WebPages.WebPageExecutingBase.NormalizePath(String path) +49 System.Web.WebPages.WebPageExecutingBase.NormalizeLayoutPagePath(String layoutPage, Func`2 fileExists) +48 System.Web.WebPages.WebPageExecutingBase.NormalizeLayoutPagePath(String layoutPage) +83 System.Web.WebPages.WebPageBase.PopContext() +197 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +195 System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +576 System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +362 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +410 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39 System.Web.Mvc.&lt;&gt;c__DisplayClass1c.&lt;InvokeActionResultWithFilters&gt;b__19() +60 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +391 System.Web.Mvc.&lt;&gt;c__DisplayClass1e.&lt;InvokeActionResultWithFilters&gt;b__1b() +61 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +285 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +831 System.Web.Mvc.Controller.ExecuteCore() +136 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +233 System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +39 System.Web.Mvc.&lt;&gt;c__DisplayClassb.&lt;BeginProcessRequest&gt;b__5() +68 System.Web.Mvc.Async.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0() +44 System.Web.Mvc.Async.&lt;&gt;c__DisplayClass8`1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _) +42 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +142 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +54 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.&lt;&gt;c__DisplayClasse.&lt;EndProcessRequest&gt;b__d() +61 System.Web.Mvc.SecurityUtil.&lt;GetCallInAppTrustThunk&gt;b__0(Action f) +31 System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +56 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +110 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +690 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +194 </pre> </td> </tr> </tbody> </table> <hr color="silver" size="1" width="100%"> <p><span face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif " style="font-family:Arial,Helvetica,Geneva,SunSans-Regular,sans-serif"><b>Version Information:</b> Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272 </span></p> 2012-04-03T18:09:57-04:004914684http://forums.asp.net/p/1788756/4914684.aspx/1?Re+mvc3+use+external+layout+pageRe: mvc3 use external layout page <p>You can't load a layout page from another domain. &nbsp;The layout page has to be contained within the solution.</p> 2012-04-03T19:37:26-04:004914780http://forums.asp.net/p/1788756/4914780.aspx/1?Re+mvc3+use+external+layout+pageRe: mvc3 use external layout page <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Xequence</h4> http:/www.xxx.com/views/shared/xxx/_layout.cshtml' is not a valid virtual path.</blockquote> <p></p> <p>You can not see directly any view in the views folder. Rather, make an action that returns a view.</p> <p></p> 2012-04-03T21:16:07-04:004914807http://forums.asp.net/p/1788756/4914807.aspx/1?Re+mvc3+use+external+layout+pageRe: mvc3 use external layout page <p>What if I am on the same domain?</p> 2012-04-03T21:54:33-04:004914931http://forums.asp.net/p/1788756/4914931.aspx/1?Re+mvc3+use+external+layout+pageRe: mvc3 use external layout page <p>has to be in the same project. &nbsp;The views have to be able to compile together. &nbsp;There are ways to get layouts to compile across multiple projects in the same solution (Orchard CMS does this) but you'll have to write a bunch of custom code to make it happen.</p> 2012-04-04T02:33:05-04:004916021http://forums.asp.net/p/1788756/4916021.aspx/1?Re+mvc3+use+external+layout+pageRe: mvc3 use external layout page <p></p> <blockquote><span class="icon-blockquote"></span> <h4>ryanw51</h4> as to be in the same project. &nbsp;The views have to be able to compile together. &nbsp;There are ways to get layouts to compile across multiple projects in the same solution (Orchard CMS does this) but you'll have to write a bunch of custom code to make it happen.</blockquote> <p></p> <p></p> <p>What if my layout has custom controller calls? Won't these fail?</p> 2012-04-04T13:48:07-04:004924139http://forums.asp.net/p/1788756/4924139.aspx/1?Re+mvc3+use+external+layout+pageRe: mvc3 use external layout page <p>Hi</p> <p>Make a VirtualPathProvider that handles virtual paths that start with a magic token and passes all other paths to its <a href="http://msdn.microsoft.com/en-us/library/system.web.hosting.virtualpathprovider.previous.aspx"> Previous property</a>. &nbsp;For example:</p> <pre class="prettyprint">public override VirtualFile GetFile(string virtualPath) { if (virtualPath.StartsWith(&quot;~/MySpecialTemplateServer&quot;)) return new MyServerVirtualFile(virtualPath); else return Previous.GetFile(virtualPath); }</pre> <p>Hope this helpful<br> Regards<br> Young Yang</p> 2012-04-10T06:47:14-04:004925105http://forums.asp.net/p/1788756/4925105.aspx/1?Re+mvc3+use+external+layout+pageRe: mvc3 use external layout page <p>I have tried doing virtual path but I am missing a few pieces of information. What is ~/myspecialtemplateserver? Is this a virtual directory in iis? When I return this file, can I specify it in my razor view as my layout?</p> <p></p> <p>Such that Layout = GetFile(&quot;webserver/iisFolder/interestingLayouts&quot;)</p> <p>or</p> <p>Layout = GetFile(&quot;remoteServer/iisFolder/christmasLayout&quot;)</p> 2012-04-10T13:50:44-04:00