gpt4 book ai didi

c# - Html 助手不使用自定义 VirtualPathProvider

转载 作者:太空狗 更新时间:2023-10-29 20:30:48 26 4
gpt4 key购买 nike

我已经设置了一个 VirtualPathProvider,它可以很好地用于地址栏中的直接 url 调用,例如 http://.../home/index

public class HomeController
{
public ActionResult Index()
{
// This triggers MyVirtualPathProvider functionallity when called via
// the browsers address bar or anchor tag click for that matter.
// But it does not trigger MyVirtualPathProvider for 'in-view' calls like
// @{ Html.RenderAction("index", "home"); }
return View();
}
}

public class MyVirtualPathProvider : VirtualPathProvider
{

public override System.Web.Hosting.VirtualFile GetFile(string virtualPath)
{
// This method gets hit after the Controller call for return View(...);
if (MyCondition)
return MyVirtualFileHandler.Get(virtualPath);
return base.GetFile(virtualPath);
}

public override bool FileExists(string virtualPath)
{
// This method gets hit after the Controller call for return View(...);
if (MyCondition)
return true;
return base.FileExists(virtualPath);
}

}

但是,我希望这也适用于 Html 助手,但现在它忽略了 View 中 html 助手调用的 VirtualPathProvider:

@{
Html.RenderAction("index", "home");
}

有没有办法解决这个问题?

此外,我有一个 WebViewPage 的覆盖,所以我可以覆盖助手的初始化,但我不知道是什么或如何。

编辑:

我已经在两台电脑上试过了,奇怪的是,它在另一台电脑上也能正常工作。所以问题实际上变成了:

为什么 VirtualPathProvider 在一台计算机上工作,而在另一台计算机上失败 50%?但随后这个问题会变得有些模糊,甚至是推测性的。尽管如此,我对此并不满意,但似乎我必须重新安装一些东西。 :(

最佳答案

用户 LostInComputer 非常友好地交出了一个在我的笔记本上运行的示例项目,我很困惑会有什么不同。

通常人们会期望 Html 助手确实只是VirtualPathProvider 工作,现在我知道它应该。

实际问题出在我遇到问题的特定电脑的安装上,重新安装后一切正常。所以这并不是一个真正复杂的解决方案,但由于很少有人关注这个问题,我至少给出了我自己的答案,因为有一天这可能对其他人有用,它可能会多么乏味。 :)

当您肯定希望某些东西能正常工作时,您总是可以尝试在另一台机器上运行它(当然如果您有可用的机器),因为最终所有可能出错的可能只是一个损坏的安装。 :(

关于c# - Html 助手不使用自定义 VirtualPathProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20608672/

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