【发布时间】:2013-09-07 02:05:43
【问题描述】:
我已经阅读了几十个相关主题,并从示例中创建了我非常简单的虚拟提供程序。
但是它不会渲染虚拟文件流。只显示计划文本。
这是输出。
@inherits System.Web.Mvc.WebViewPage
@{ViewBag.Title = "Hellow World !";}
<h2>Hellow World !</h2>
有关于这个的相关线程,但他们没有说他们是如何解决它或解决方案不起作用的。我找不到我做错了什么。
- VirtualPathProvider not parsing razor markup
- MVC3 Custom VirtualPathProvider not rendering Razor
- Pulling a View from a database rather than a file
- How to specify route to shared views in MVC3 when using Areas
- ASP.NET MVC load Razor view from database
- How to load views from a Class Library project?
还有更多...
怎么了?
这是我的测试代码。 (Global.asax,这就是我所做的全部更改。)
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(
new MyProvider());
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
}
public class MyProvider : VirtualPathProvider
{
public override bool FileExists(string virtualPath)
{
if (Previous.FileExists(virtualPath))
return true;
else
// ~/Infra is the test url
return virtualPath.StartsWith("/Infra") || virtualPath.StartsWith("~/Infra");
}
public override VirtualFile GetFile(string virtualPath)
{
if (Previous.FileExists(virtualPath))
return base.GetFile(virtualPath);
else
return new MyVirtualFile(virtualPath);
}
public class MyVirtualFile : VirtualFile
{
public MyVirtualFile(string virtualPath) : base(virtualPath) { }
public override Stream Open()
{
//Loading stream from seperate dll shows just plain text
//System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(
// System.IO.Path.Combine(HttpRuntime.BinDirectory, "Infra.dll"));
//return assembly.GetManifestResourceStream("Infra.Views.Home.Index.cshtml");
//Changed to string but also it shows plain text.
return new System.IO.MemoryStream(System.Text.ASCIIEncoding.UTF8.GetBytes(
"@inherits System.Web.Mvc.WebViewPage \r\n <h2>Hellow World !</h2>"));
}
}
}
【问题讨论】: