gpt4 book ai didi

virtualpathprovider - mvc4 razor 中是否有任何 VirtualPathProvider 工作示例?

转载 作者:行者123 更新时间:2023-12-02 02:05:23 25 4
gpt4 key购买 nike

我已经阅读了数十个相关线程,并根据示例制作了我非常简单的虚拟提供程序。

但是它不渲染虚拟文件流。仅显示计划文本。

这是输出。

@inherits System.Web.Mvc.WebViewPage 
@{ViewBag.Title = "Hellow World !";}
<h2>Hellow World !</h2>

关于这个有相关的线程,但他们没有说他们是如何解决它的或者解决方案不起作用。我找不到我做错了什么。

还有很多...

怎么了?

这是我的测试代码。 (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>"));
}
}
}

最佳答案

我可以看出这个问题有点老了,但我刚刚遇到了同样的错误。我认为问题出在测试 URL 上。我没有时间进行全面调查,但我认为除非提供的 URL 是预期的格式(ASP.NET MVC View 引擎是基于约定的),否则它可能不会使用 razor 作为 View 引擎;我不确定这是否是原因,但一些使用您正在使用的“Infra”字符串的示例:

家庭 Controller 中的新 MVC 4 项目:

public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";

dynamic x = new ExpandoObject();

return View("Infra-test.cshtml", x);
}

这调用到:

private bool IsPathVirtual(string virtualPath)

virtualPath 设置为 '/Views/Home/Infra-test.cshtml.aspx'。它在末尾添加了一个 aspx 扩展,这让我相信它没有使用 razor 来编译 View 。对虚拟路径提供程序的一个小修改将看到下面的链接工作:

public override bool FileExists(string virtualPath)
{
if (Previous.FileExists(virtualPath))
return true;
else
// prevent view start compilation errors
return virtualPath.StartsWith("/Infra") && !virtualPath.Contains("_ViewStart");
}

有效的 URL:

return View("/Infra/test.cshtml", x);
return View("/Infra/one/test.cshtml", x);
return View("/Infra/one/two/test.cshtml", x);

这些不起作用:

return View("/Infra", x);
return View("/Infra/test", x);

要使示例正常工作,您还需要实现 GetCacheDependency;否则,在磁盘上找不到虚拟路径的文件时会抛出异常,下面是一个简单的例子。阅读文档以正确实现。

private bool IsVirtualPath(string virtualPath)
{
return virtualPath.StartsWith("/Infra") && !virtualPath.Contains("_ViewStart");
}

public override CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
{
if (IsVirtualPath(virtualPath))
return null;

return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}

关于virtualpathprovider - mvc4 razor 中是否有任何 VirtualPathProvider 工作示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15332247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com