如何使用 ASP.NET MVC 获取当前视图的物理文件夹?

发布于 2024-08-15 02:11:43 字数 422 浏览 5 评论 0原文

我有一个在控制器中调用的单独的实用程序类,并且我需要访问 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末が日狂欢 2024-08-22 02:11:43

在弄乱了 RouteValueDictionary、RouteData、RequestContext 和各种 Route 类之后,我几乎准备放弃并硬编码我的方式。我不久前开始了这个项目,所以我忘记了我所做的 WebFormViewEngine 类的自定义。我将继续发布我的解决方案,尽管我意识到它可能不是最优雅、最安​​全或最实用的(就最佳实践而言)。

首先,我扩展了 WebFormViewEngine 类并覆盖了 FindView 方法:

public class CustomViewEngine : WebFormViewEngine
{
 public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
 {
  ViewEngineResult result = null;
  var request = controllerContext.HttpContext.Request;

  // modify stuff here

  result = base.FindView(controllerContext, viewName, masterName, useCache);

  return result;
 }
}

我所做的就是向我的实用程序类添加一个静态属性,如下所示:

public static string CurrentViewPath { get; set; }

并修改 FindView 方法以捕获 ViewEngineResult 并获取 ViewPath:

public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
  ViewEngineResult result = null;
  var request = controllerContext.HttpContext.Request;

  // modify stuff here

  result = base.FindView(controllerContext, viewName, masterName, useCache);

  System.Web.Mvc.WebFormView wfvView = (System.Web.Mvc.WebFormView)result.View;
  HelperFunctions.CurrentViewPath = wfvView.ViewPath.Replace(viewName + ".aspx","");

  return result;
}

这给了我虚拟的通往视图的路径,这正是我所需要的。剩下要做的唯一一件事就是返回到实用程序类并使用 HttpContext.Current.Request.MapPath 方法来获取当前视图文件所在位置的完整物理路径:

string ViewPath = HttpContext.Current.Request.MapPath(CurrentViewPath);

宾果!

这是一种迂回的黑客方式来做到这一点,嘿,如果它有效的话......

感谢大家的帮助和有用的建议。

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:

public class CustomViewEngine : WebFormViewEngine
{
 public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
 {
  ViewEngineResult result = null;
  var request = controllerContext.HttpContext.Request;

  // modify stuff here

  result = base.FindView(controllerContext, viewName, masterName, useCache);

  return result;
 }
}

What i did was add a static property to my utility class, like so:

public static string CurrentViewPath { get; set; }

and modified the FindView method to capture the ViewEngineResult and get the ViewPath:

public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
  ViewEngineResult result = null;
  var request = controllerContext.HttpContext.Request;

  // modify stuff here

  result = base.FindView(controllerContext, viewName, masterName, useCache);

  System.Web.Mvc.WebFormView wfvView = (System.Web.Mvc.WebFormView)result.View;
  HelperFunctions.CurrentViewPath = wfvView.ViewPath.Replace(viewName + ".aspx","");

  return result;
}

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:

string ViewPath = HttpContext.Current.Request.MapPath(CurrentViewPath);

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.

谁与争疯 2024-08-22 02:11:43

该框架固执己见,因此我会使用该框架使用的相同搜索算法。找到应用程序的根目录,然后查看视图文件夹。它应该位于与相关控制器关联的文件夹中或共享文件夹中。使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文