Asp.net mvc 3 从db加载Razor视图,但ViewBag已损坏
我正在从数据库中提取razor视图的标记,如本问题所述: 我可以拉取视图,但执行时失败,因为无法识别ViewBag CS0103:名称“ViewBag”在当前上下文中不存在 有什么建议吗 以下是消息来源: 全球:Asp.net mvc 3 从db加载Razor视图,但ViewBag已损坏,asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,我正在从数据库中提取razor视图的标记,如本问题所述: 我可以拉取视图,但执行时失败,因为无法识别ViewBag CS0103:名称“ViewBag”在当前上下文中不存在 有什么建议吗 以下是消息来源: 全球: protected void Application_Start() { AreaRegistration.RegisterAllAreas(); System.Web.Hosting.HostingEnvironmen
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new BearForce.Web.Core.DbPathProvider());
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
我的路径提供程序:
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();
}
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Name = "John";
return View();
}
}
显然,这是一个概念证明,因此名称都很愚蠢,代码也很潦草……您应该确保返回的视图与razor视图相对应。下面是一个简化的工作示例:
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);
}
}
它将在应用程序\u Start
中注册:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
HostingEnvironment.RegisterVirtualPathProvider(new CustomPathProvider());
}
最后是一个示例控制器:
public ActionResult Index()
{
ViewBag.Title = "aaah!!! Muppets!!! Help!!!!!";
return View();
}
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Name = "John";
return View();
}
}
最后一条非常重要的意见是,如果您正在实施定制:
如果您的web应用程序是预编译的,则这不起作用。因此,如果您正在使用预编译(如发布…或Web部署项目),您的自定义虚拟路径提供程序将永远不会被使用。对于将来遇到此错误的人,如果您的视图和根项目文件夹中缺少Web.config文件,您可能会遇到此错误。+1很高兴我在此处滚动。这正是我错过的!如果使用源代码管理而忘记提交.config文件,可能很常见:)谢谢,解决了我的问题!我发现视图中的web.config被标记为buildaction:None而不是Content,因此它没有被发布。这是在将MVC3安装到Webforms项目中之后发生的。但这与问题无关。