Note that there are some explanatory texts on larger screens.

plurals
  1. POLoad Razor view from db, but ViewBag is broken
    primarykey
    data
    text
    <p>I'm pulling a razor view's markup from the database, as detailed in this question:</p> <p><a href="https://stackoverflow.com/questions/4218454/asp-net-mvc-load-razor-view-from-database">ASP.NET MVC load Razor view from database</a></p> <p>I can pull the view, but it fails on execution because ViewBag is not recognized. </p> <p>CS0103: The name 'ViewBag' does not exist in the current context</p> <p>Any suggestions?</p> <p>Here's the source:</p> <p>global:</p> <pre><code>protected void Application_Start() { AreaRegistration.RegisterAllAreas(); System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new BearForce.Web.Core.DbPathProvider()); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } </code></pre> <p>my path provider:</p> <pre><code>namespace BearForce.Web.Core { public class DbPathProvider : VirtualPathProvider { public DbPathProvider() : base() { } public override bool FileExists(string virtualPath) { var repo = new Repository(); var viewPage = repo.GetView(240, virtualPath); if (base.FileExists(virtualPath)) { return true; } if (viewPage != null) { return true; } return false; } public override VirtualFile GetFile(string virtualPath) { if (base.FileExists(virtualPath)) { return base.GetFile(virtualPath); } var repo = new Repository(); var result = repo.GetView(240, virtualPath); var vf = new DbVirtualFile(virtualPath, result.Markup); return vf; } } } </code></pre> <p>my Virtual File:</p> <pre><code> public class DbVirtualFile : System.Web.Hosting.VirtualFile { string _fileContents = string.Empty; public DbVirtualFile(string path, string fileContents) : base(path) { _fileContents = fileContents; } public override System.IO.Stream Open() { return new System.IO.MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(_fileContents)); } } </code></pre> <p>My Controller:</p> <pre><code> public ActionResult Index() { ViewBag.Title = "aaah!!! Muppets!!! Help!!!!!"; return View(); } </code></pre> <p>Obviously, this is a proof of concept, so the names are all silly and the code sloppy as hell...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload