How can I find a FindPartialView search inside my District?

So, I register all areas in Global.asax :

 protected void Application_Start() { AreaRegistration.RegisterAllAreas(); //... RouteConfig.RegisterRoutes(RouteTable.Routes); } 

But in my /Areas/Log/Controllers , when I try to find PartialView :

 ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, "_LogInfo"); 

Failed viewResult.SearchedLocations :

 "~/Views/Log/_LogInfo.aspx" "~/Views/Log/_LogInfo.ascx" "~/Views/Shared/_LogInfo.aspx" "~/Views/Shared/_LogInfo.ascx" "~/Views/Log/_LogInfo.cshtml" "~/Views/Log/_LogInfo.vbhtml" "~/Views/Shared/_LogInfo.cshtml" "~/Views/Shared/_LogInfo.vbhtml" 

And thus viewResult.View is null .

How can I search for FindPartialView in my region?

Update : This is my custom viewer that I registered with Global.asax :

 public class MyCustomViewEngine : RazorViewEngine { public MyCustomViewEngine() : base() { AreaPartialViewLocationFormats = new[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml" }; PartialViewLocationFormats = new[] { "~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" }; // and the others... } } 

But FindPartialView does not use AreaPArtialViewLocationFormats :

 "~/Views/Log/_LogInfo.cshtml" "~/Views/Shared/_LogInfo.cshtml" 
+4
source share
1 answer

I had exactly the same problem, I have a central Ajax controller that I use, in which I return different partial views from different folders / locations.

What you need to do is create a new ViewEngine derived from RazorViewEngine (I assume you are using Razor), and explicitly include the new locations in the constructor to search for scores in.

Alternatively, you can override the FindPartialView method. By default, the Shared folder and the folder from the current controller context are used for search.

Here is an example that shows how to override certain properties in a custom RazorViewEngine .

Update

You must specify the partial path in your PartialViewLocationFormats array as follows:

 public class MyViewEngine : RazorViewEngine { public MyViewEngine() : base() { PartialViewLocationFormats = new string[] { "~/Area/{0}.cshtml" // .. Other areas .. }; } } 

Similarly, if you want to find partial in the controller inside the Area folder, you will need to add standard partial views to the AreaPartialViewLocationFormats array. I tested this and it works for me.

Remember to add a new RazorViewEngine to your Global.asax.cs , for example:

 protected void Application_Start() { // .. Other initialization .. ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new MyViewEngine()); } 

Here's how you can use it in an example controller called "Home":

 // File resides within '/Controllers/Home' public ActionResult Index() { var pt = ViewEngines.Engines.FindPartialView(ControllerContext, "Partial1"); return View(pt); } 

I saved the part I'm looking for in the /Area/Partial1.cshtml path.

+2
source

All Articles