I have been reading some very good post from Wooncherk, Twisted Whisperer, and Alex Wolf. Their articles respectively (Wooncherk), (Twisted Whisperer), and (Alex Wolf) have been extremely informative but alas I'm just not as smart as the rest of the SO community and can't quite piece together what I'm missing.
I am overriding a core view in the admin area...specifically the order edit view. The behavior I am seeing is that it does NOT hit the controller in my plugin but it DOES display my custom view. Problem is that the custom view is in the Admin project and this really confuses me. How can I have a self-contained plugin yet have to install my custom view into the core Admin area?
I thought, perhaps incorrectly, that what would happen is that my controller would be hit first when paths are being searched because of my higher defined priority.
So following the directions here is my code.
CustomViewEngine:
private readonly string[] _emptyLocations = null;
public CustomViewEngine()
{
PartialViewLocationFormats = new[]
{
"~/Administration/MyExtension/Views/{1}/{0}.cshtml",
"~/Administration/MyExtension/Views/Shared/{0}.cshtml"
};
ViewLocationFormats = new[]
{
"~/Administration/MyExtension/Views/{1}/{0}.cshtml",
"~/Administration/MyExtension/Views/Shared/{0}.cshtml"
};
}
protected override string GetPath(ControllerContext controllerContext, string[] locations, string[] areaLocations, string locationsPropertyName, string name, string controllerName, string theme, string cacheKeyPrefix, bool useCache, out string[] searchedLocations)
{
searchedLocations = _emptyLocations;
if (string.IsNullOrEmpty(name))
{
return string.Empty;
}
string areaName = GetAreaName(controllerContext.RouteData);
//little hack to get nop's admin area to be in /Administration/ instead of /Nop/Admin/ or Areas/Admin/
if (!string.IsNullOrEmpty(areaName) && areaName.Equals("admin", StringComparison.InvariantCultureIgnoreCase))
{
var newLocations = areaLocations.ToList();
newLocations.Insert(0, "~/Administration/Views/{1}/{0}.cshtml");
newLocations.Insert(0, "~/Administration/Views/Shared/{0}.cshtml");
//Insert your custom View locations to the top of the list to be given a higher precedence
newLocations.Insert(0, "~/Administration/MyExtension/Views/{1}/{0}.cshtml");
newLocations.Insert(0, "~/Administration/MyExtension/Views/Shared/{0}.cshtml");
areaLocations = newLocations.ToArray();
}
bool flag = !string.IsNullOrEmpty(areaName);
List<ViewLocation> viewLocations = GetViewLocations(locations, flag ? areaLocations : null);
if (viewLocations.Count == 0)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Properties cannot be null or empty.", new object[] { locationsPropertyName }));
}
bool flag2 = IsSpecificPath(name);
string key = CreateCacheKey(cacheKeyPrefix, name, flag2 ? string.Empty : controllerName, areaName, theme);
if (useCache)
{
var cached = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, key);
if (cached != null)
{
return cached;
}
}
if (!flag2)
{
return GetPathFromGeneralName(controllerContext, viewLocations, name, controllerName, areaName, theme, key, ref searchedLocations);
}
return GetPathFromSpecificName(controllerContext, name, key, ref searchedLocations);
}
Route Provider See Comments
Trying to override the core url ~/Admin/Order/Edit/1
public void RegisterRoutes(RouteCollection routes)
{
ViewEngines.Engines.Insert(0, new CustomViewEngine());
//the articles above did NOT mention adding a path but it seemed like I needed to in order for my override path to be included???
routes.MapRoute("Plugin...OrderDetailsOverride", "Order/Edit/{id}",
new { controller = "MyOrder", action = "Edit" },
new { id = @"d+" },
new[] { "MyPlugin.Controllers" }
);
}
public int Priority
{
get
{
return 1;
}
}
I was getting a 404 error and carefully rereading Twisted Whisperer he(I assume he) states:
For example, if you are overrding Nop.Admin/Views/Category/Tree.cshtml, place your custom Tree.cshtml in Nop.Admin/CustomExtension/Views/Category/Tree.cshtml
Well if I interpret that literally I would do this in the CORE ADMIN project:
I obviously did this and it worked...sorta.
So in summary my questions / issues are:
My Order Override Controller in the plugin isn't hit....(not interested in using ActionFilters as they don't give me what I need...I don't think).
The view in my plugin isn't hit but the one added to the admin project is hit?
Somewhat related to 2. If two is correct then how is that a viable plugin solution? For a production push, updates, etc. I would potentially have to touch the NopCommerce core projects....well why bother with a plugin then?
Now the NOP guys as well as SO Community are far wiser than I so I'm sure I just don't understand properly.
See Question&Answers more detail:
os