如何使用 ASP.NET MVC 获取当前视图的物理文件夹?
我有一个在控制器中调用的单独的实用程序类,并且我需要访问 Views 文件夹内的文件。 Views{controller} 内部有几个包含不同视图的子文件夹。我正在寻找类似于使用:
HttpContext.Current.Request.PhysicalApplicationPath
获取驱动器上的物理文件夹。
我尝试使用:
HttpContext.Current.Request.MapPath(HttpContext.Current.Request.Path);
但这只是将请求的路线映射到实际上并不存在的物理路径(即,如果我请求“/Cars/Models/123”,它将返回“C:\AppPath\Cars\Models\123\” ')
有什么想法吗?
I have a separate utility class that is called in the controller, and i need to access a file inside the Views folder. Inside the the Views{controller} are a couple of sub folders that contain the different views. What I'm looking for is something similar to using:
HttpContext.Current.Request.PhysicalApplicationPath
to get the physical folder on the drive.
I tried using:
HttpContext.Current.Request.MapPath(HttpContext.Current.Request.Path);
but that just maps the requested route to a physical path that doesn't actually exits (ie. if i request '/Cars/Models/123' it will return 'C:\AppPath\Cars\Models\123\')
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在弄乱了 RouteValueDictionary、RouteData、RequestContext 和各种 Route 类之后,我几乎准备放弃并硬编码我的方式。我不久前开始了这个项目,所以我忘记了我所做的 WebFormViewEngine 类的自定义。我将继续发布我的解决方案,尽管我意识到它可能不是最优雅、最安全或最实用的(就最佳实践而言)。
首先,我扩展了 WebFormViewEngine 类并覆盖了 FindView 方法:
我所做的就是向我的实用程序类添加一个静态属性,如下所示:
并修改 FindView 方法以捕获 ViewEngineResult 并获取 ViewPath:
这给了我虚拟的通往视图的路径,这正是我所需要的。剩下要做的唯一一件事就是返回到实用程序类并使用 HttpContext.Current.Request.MapPath 方法来获取当前视图文件所在位置的完整物理路径:
宾果!
这是一种迂回的黑客方式来做到这一点,嘿,如果它有效的话......
感谢大家的帮助和有用的建议。
After messing around with RouteValueDictionary, RouteData, RequestContext and all sorts of Route classes, I was almost ready to give up and hardcode my way in. I started this project a while ago so i had forgotten about the customizations to the WebFormViewEngine class i had done. I'm gonna go ahead and post my solution, even though i realize it may not be the most elegant, secure or practical (in terms of best practices).
First of all i had extendend the WebFormViewEngine class and overridden the FindView method:
What i did was add a static property to my utility class, like so:
and modified the FindView method to capture the ViewEngineResult and get the ViewPath:
this gave me the virtual path to the view, which is just what i needed. the only thing left to do was go back to the utility class and use the HttpContext.Current.Request.MapPath method to get the full physical path of where the current view file is:
And bingo!
It is sort of a roundabout hackish way to do this, bu hey, if it works...
Thanks for the help and useful suggestions everyone.
该框架固执己见,因此我会使用该框架使用的相同搜索算法。找到应用程序的根目录,然后查看视图文件夹。它应该位于与相关控制器关联的文件夹中或共享文件夹中。使用 RouteValueDictionary 来访问控制器。
The framework is opinionated so I'd use the same search algorithm the framework uses. Find the root of the app, then look in the views folder. It should be either in the folder associated with the controller in question or in the shared folder. Use the RouteValueDictionary to get access to the controller.