c# - Specifying Area to search for Views in -
i have mvc4 app uses 1 area (in addition normal location). working fine routing, have need code in 'root' location find partial view inside area.
the relevant parts of solution's files are:
/areas /areas/admin /areas/admin/views /areas/admin/views/shared/_adminpartialview.cshtml /views /views/shared/_rootpartialview.cshtml
and failing code is:
var viewengine = new razorviewengine(); var ccontext = new controllercontext(context, new routedata(), new emptycontroller()); // works: var rootview = viewengine.findpartialview(ccontext, "_rootpartialview", false); if (rootview == null) { throw new exception("root view not found"); } // throws error: var adminview = viewengine.findpartialview(ccontext, "_adminpartialview", false); if (adminview == null) { throw new exception("admin view not found"); }
(context
above coming indirectly httpcontext.current)
... makes sense, without area specified admin area shouldn't searched... when change viewengine
above = new adminrazorviewengine();
, defined as:
public class adminrazorviewengine : razorviewengine { public adminrazorviewengine() { var viewlocations = new [] { "~/areas/admin/views/{1}/{0}.cshtml", "~/areas/admin/views/shared/{0}.cshtml" }; base.viewlocationformats = viewlocations.concat(base.viewlocationformats).toarray(); } }
i still same results.
if copy _adminpartialview.cshtml
/views/shared/
both views found correctly, using either view engine.
i've tried creating actual routedata object, specifying area on it, , using when constructing controllercontext:
var routedata = new routedata(); routedate.values.add("area", "admin"); // ... var ccontext = new controllercontext(context, routedata, new emptycontroller());
with no change in behavior.
how can explicitly specify area view engine search in?
try setting areaviewlocationformats
instead of viewlocationformats
here code ilspy razorviewengine
public razorviewengine(iviewpageactivator viewpageactivator) : base(viewpageactivator) { base.areaviewlocationformats = new string[] { "~/areas/{2}/views/{1}/{0}.cshtml", "~/areas/{2}/views/{1}/{0}.vbhtml", "~/areas/{2}/views/shared/{0}.cshtml", "~/areas/{2}/views/shared/{0}.vbhtml" }; base.areamasterlocationformats = new string[] { "~/areas/{2}/views/{1}/{0}.cshtml", "~/areas/{2}/views/{1}/{0}.vbhtml", "~/areas/{2}/views/shared/{0}.cshtml", "~/areas/{2}/views/shared/{0}.vbhtml" }; base.areapartialviewlocationformats = new string[] { "~/areas/{2}/views/{1}/{0}.cshtml", "~/areas/{2}/views/{1}/{0}.vbhtml", "~/areas/{2}/views/shared/{0}.cshtml", "~/areas/{2}/views/shared/{0}.vbhtml" }; base.viewlocationformats = new string[] { "~/views/{1}/{0}.cshtml", "~/views/{1}/{0}.vbhtml", "~/views/shared/{0}.cshtml", "~/views/shared/{0}.vbhtml" }; base.masterlocationformats = new string[] { "~/views/{1}/{0}.cshtml", "~/views/{1}/{0}.vbhtml", "~/views/shared/{0}.cshtml", "~/views/shared/{0}.vbhtml" }; base.partialviewlocationformats = new string[] { "~/views/{1}/{0}.cshtml", "~/views/{1}/{0}.vbhtml", "~/views/shared/{0}.cshtml", "~/views/shared/{0}.vbhtml" }; base.fileextensions = new string[] { "cshtml", "vbhtml" }; }
Comments
Post a Comment