从数据库加载的Razor视图,但ViewBag被打破 [英] Load Razor view from db, but ViewBag is broken

查看:492
本文介绍了从数据库加载的Razor视图,但ViewBag被打破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从数据库中提取剃刀视图的标记,因为在这个问题详细:

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

ASP.NET MVC从数据库负荷的Razor视图

ASP.NET MVC load Razor view from database

我可以拉的观点,但在执行失败,因为ViewBag无法识别。

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

CS0103:'ViewBag'这个名字并不在目前的情况下存在

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

有什么建议?

下面是源:

全球

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


    }
}

我的虚拟文件:

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

我的控制器:

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

            return View();
        }

显然,这是一个概念证明,所以名字都是愚蠢和code马虎的地狱...

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

推荐答案

您应该确保该视图要返回对应于剃刀视图。这里有一个简单的例子:

You should make sure that the view you are returning corresponds to a razor view. Here's a simplified working example:

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

这将在的Application_Start 注册:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
    HostingEnvironment.RegisterVirtualPathProvider(new CustomPathProvider());
}

和最后一个样本控制器:

and finally a sample controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Name = "John";
        return View();
    }
}

如果你要实现自定义的的VirtualPathProvider

如果您的web应用程序是precompiled这是行不通的。所以,如果你正在使用precompilation(之类的东西...发布或Web部署项目)自定义虚拟路径提供将永远不会被使用。

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.

这篇关于从数据库加载的Razor视图,但ViewBag被打破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆