ASP .NET MVC VirtualPathProvider
我正在编写一个 VirtualPathProvider 来动态加载我的 MVC 视图,这些视图位于不同的目录中。我在 MVC 之前成功拦截了调用(在 FileExists 中),但是在我的 VirtualPathProvider 中,我得到了原始的、预先路由的 url,例如:
~/Apps/Administration/Account/LogOn
就个人而言,我知道 MVC 会查找
~/Apps/Administration/Views/Account/ LogOn.aspx
并且我应该从中读取文件内容
D:\SomeOtherNonWebRootDirectory\Apps\Administration\Views\Account\LogOn.aspx
,但我不想将逻辑硬编码为“添加名为 Views 的目录并将 aspx 添加到末尾”。
该逻辑存储在哪里以及如何将其放入我的虚拟路径提供程序中?
谢谢。抱歉,如果我没说清楚。
I am writing a VirtualPathProvider to dynamically load my MVC views, which are located in a different directory. I successfully intercept the call before MVC (in FileExists), but in my VirtualPathProvider, I get the raw, pre-routed url like:
~/Apps/Administration/Account/LogOn
Personally, I know that MVC will look for
~/Apps/Administration/Views/Account/LogOn.aspx
and that I should be reading the file contents from
D:\SomeOtherNonWebRootDirectory\Apps\Administration\Views\Account\LogOn.aspx
but I'd rather not hard code the logic to "add the directory named Views and add aspx to the end".
Where is this logic stored and how can I get it into my virtual path provider?
Thanks. Sorry if I'm not being clear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑
您需要创建一个继承
WebFormViewEngine
并设置ViewLocationFormats
属性(继承自 VirtualPathProviderViewEngine)的类。默认值可以在 MVC 源代码中找到:
然后您应该清除
ViewEngines.Engines
集合并向其中添加您的 ViewEngine 实例。Edited
You need to make a class that inherits
WebFormViewEngine
and sets theViewLocationFormats
property (inherited from VirtualPathProviderViewEngine).The default values can be found in the MVC source code:
You should then clear the
ViewEngines.Engines
collection and add your ViewEngine instance to it.正如上面的 SLAks 提到的,您需要创建一个自定义视图引擎并在 FindView 方法中添加您的查找逻辑。
{
}
在 Application_Start 中,您可以像这样注册您的视图引擎:
As SLaks mentioned above, you need to create a Custom View Engine and add your view-finding logic in the FindView method.
{
}
In the Application_Start, you can register your View Engine like this:
答案是 MVC 没有正确找到我的控制器。如果 MVC 实际上正确找到了您的控制器,则 VirtualPathProvider 应该处理两个请求:
带有所请求的实际 url 的初始请求(即 http://.../Account/LogOn)。
随后的 FileExists 检查 http://.../Views/Account/LogOn。 aspx,1.中的请求后调用FileExists返回false。这实际上会重新调整 aspx 内容。
The answer was that MVC was not finding my controller properly. If MVC does in fact find your controller properly, there should be two requests processed by the VirtualPathProvider:
An initial request with the acutal url requested (ie. http://.../Account/LogOn).
A subsequent FileExists check for http://.../Views/Account/LogOn.aspx, after the request in 1. returns false calling FileExists. This actually retuns the aspx content.