Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should make sure that the view you are returning corresponds to a razor view. Here's a simplified working example:</p> <pre><code>public class CustomPathProvider : VirtualPathProvider { private class CustomVirtualFile : VirtualFile { public CustomVirtualFile(string path) : base(path) { } public override Stream Open() { return new MemoryStream(Encoding.UTF8.GetBytes("Hello @ViewBag.Name")); } } public override bool FileExists(string virtualPath) { // This is very important: make sure that here you // are returning true only for Razor view pages or // you won't have ViewBag. // In this oversimplified example we support // the index view for the home controller return virtualPath == "/Views/Home/Index.cshtml"; } public override VirtualFile GetFile(string virtualPath) { return new CustomVirtualFile(virtualPath); } } </code></pre> <p>which would be registered in <code>Application_Start</code>:</p> <pre><code>protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); HostingEnvironment.RegisterVirtualPathProvider(new CustomPathProvider()); } </code></pre> <p>and finally a sample controller:</p> <pre><code>public class HomeController : Controller { public ActionResult Index() { ViewBag.Name = "John"; return View(); } } </code></pre> <p>And a final very important remark if you are implementing a custom <a href="http://msdn.microsoft.com/en-us/library/system.web.hosting.virtualpathprovider.aspx" rel="nofollow">VirtualPathProvider</a>:</p> <p>This doesn't work if your web application is precompiled. So if you are using precompilation (things like Publish... or Web Deployment Projects) your custom virtual path provider will never be used.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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