English 中文(简体)
How to get the physical folder of the current view with ASP.NET MVC?
原标题:

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:AppPathCarsModels123 )

Any ideas?

最佳回答

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.

问题回答

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.





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签