Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
665 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - Load Razor view from db, but ViewBag is broken

I'm pulling a razor view's markup from the database, as detailed in this question:

ASP.NET MVC load Razor view from database

I can pull the view, but it fails on execution because ViewBag is not recognized.

CS0103: The name 'ViewBag' does not exist in the current context

Any suggestions?

Here's the source:

global:

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new BearForce.Web.Core.DbPathProvider());
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

my path provider:

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;
        }


    }
}

my Virtual File:

  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));
        }
    }

My Controller:

  public ActionResult Index()
        {
            ViewBag.Title = "aaah!!! Muppets!!! Help!!!!!";

            return View();
        }

Obviously, this is a proof of concept, so the names are all silly and the code sloppy as hell...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For future people who get this error, you can get this exact error if your web.config files are missing from your Views and your root project folder.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...