Using admin theme from within a plugin (v2.0)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
I’m working on a new plugin for nopCommerce 2.0 and I have some non-standard administration views, and I would like them to be styled like the standard administration views. I spent last night digging through the code and this is a question I don’t think I can answer on my own. I looked at most of the views from other plugins, but I couldn’t find examples. I would like to avoid making modifications outside of my plugin, but I will if necessary.

If I do not specify a layout in my plugin view then it will use the default public layout, and everything works just fine. Here are the results for a couple of my attempts:

Attempt #1

@{
    Layout = "~/Administration/Views/Shared/_AdminLayout.cshtml";
}


Results:
The partial view 'Menu' was not found or no view engine supports the searched locations. The following locations were searched:
~/Themes/www.StrivingProgrammers.com-DeepSkyBlue/Views/Admin/Menu.cshtml
~/Themes/www.StrivingProgrammers.com-DeepSkyBlue/Views/Admin/Menu.vbhtml
~/Themes/www.StrivingProgrammers.com-DeepSkyBlue/Views/Shared/Menu.cshtml
~/Themes/www.StrivingProgrammers.com-DeepSkyBlue/Views/Shared/Menu.vbhtml
~/Views/Admin/Menu.cshtml
~/Views/Admin/Menu.vbhtml
~/Views/Shared/Menu.cshtml
~/Views/Shared/Menu.vbhtml
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The partial view 'Menu' was not found or no view engine supports the searched locations. The following locations were searched:
~/Themes/www.StrivingProgrammers.com-DeepSkyBlue/Views/Admin/Menu.cshtml
~/Themes/www.StrivingProgrammers.com-DeepSkyBlue/Views/Admin/Menu.vbhtml
~/Themes/www.StrivingProgrammers.com-DeepSkyBlue/Views/Shared/Menu.cshtml
~/Themes/www.StrivingProgrammers.com-DeepSkyBlue/Views/Shared/Menu.vbhtml
~/Views/Admin/Menu.cshtml
~/Views/Admin/Menu.vbhtml
~/Views/Shared/Menu.cshtml
~/Views/Shared/Menu.vbhtml

Source Error:

Line 76:             {
Line 77:                 <div class="header-menu">
Line 78:                     @Html.Partial("Menu")
Line 79:                 </div>
Line 80:             }

Source File: c:\aa_src\skylerseverns_nc\Presentation\Nop.Web\Administration\Views\Shared\_AdminLayout.cshtml    Line: 78

Stack Trace:

[InvalidOperationException: The partial view 'Menu' was not found or no view engine supports the searched locations. The following locations were searched:
~/Themes/www.StrivingProgrammers.com-DeepSkyBlue/Views/Admin/Menu.cshtml
~/Themes/www.StrivingProgrammers.com-DeepSkyBlue/Views/Admin/Menu.vbhtml
~/Themes/www.StrivingProgrammers.com-DeepSkyBlue/Views/Shared/Menu.cshtml
~/Themes/www.StrivingProgrammers.com-DeepSkyBlue/Views/Shared/Menu.vbhtml
~/Views/Admin/Menu.cshtml
~/Views/Admin/Menu.vbhtml
~/Views/Shared/Menu.cshtml
~/Views/Shared/Menu.vbhtml]
   System.Web.Mvc.HtmlHelper.FindPartialView(ViewContext viewContext, String partialViewName, ViewEngineCollection viewEngineCollection) +382338
   System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection) +266
   System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) +113
   System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName) +31
   ASP._Page_Administration_Views_Shared__AdminLayout_cshtml.Execute() in c:\aa_src\skylerseverns_nc\Presentation\Nop.Web\Administration\Views\Shared\_AdminLayout.cshtml:78
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +81
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +88
   System.Web.WebPages.<>c__DisplayClass7.<RenderPageCore>b__6(TextWriter writer) +102
   System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +12
   System.Web.WebPages.WebPageBase.Write(HelperResult result) +67
   System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +66
   System.Web.WebPages.WebPageBase.PopContext() +262
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +95
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
   System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8920029
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184


Attempt #2

@{
    Layout = "~/Administration/Views/_ViewStart.cshtml";
}


Results:
The file "~/Administration/Views/_ViewStart.cshtml" could not be rendered, because it does not exist or is not a valid page.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The file "~/Administration/Views/_ViewStart.cshtml" could not be rendered, because it does not exist or is not a valid page.

Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[HttpException (0x80004005): The file "~/Administration/Views/_ViewStart.cshtml" could not be rendered, because it does not exist or is not a valid page.]
   System.Web.WebPages.Util.EnsureValidPageType(WebPageBase page, String virtualPath) +99183
   System.Web.WebPages.WebPageBase.PopContext() +166
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +95
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +220
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +303
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +23
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +260
   System.Web.Mvc.<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b() +19
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +343
   System.Web.Mvc.Controller.ExecuteCore() +116
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +97
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +37
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +12
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +50
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8920029
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
12 years ago
Seems that embedded views can't "find" .cshtml files of the main web projects (Nop.Web or Nop.Admin)
12 years ago
a.m. wrote:
Seems that embedded views can't "find" .cshtml files of the main web projects (Nop.Web or Nop.Admin)


Thank you for the response Andrei. What should the next steps be? Should I file this as a defect on codeplex, and to take it a step further would you be able to point me in the right direction for solving the issue? I could then fix it and give you the code to commit back to the trunk.
12 years ago
skyler.severns wrote:
Seems that embedded views can't "find" .cshtml files of the main web projects (Nop.Web or Nop.Admin)

Thank you for the response Andrei. What should the next steps be? Should I file this as a defect on codeplex, and to take it a step further would you be able to point me in the right direction for solving the issue? I could then fix it and give you the code to commit back to the trunk.


It appears I've fixed it. Inside of the ThemableRazorViewEngine I updated the view location lists to contain entries for the administration section.



            ViewLocationFormats = new[]
                                      {
                                            //themes
                                            "~/Themes/{2}/Views/{1}/{0}.cshtml",
                                            "~/Themes/{2}/Views/{1}/{0}.vbhtml",
                                            "~/Themes/{2}/Views/Shared/{0}.cshtml",
                                            "~/Themes/{2}/Views/Shared/{0}.vbhtml",

                                            //default
                                            "~/Views/{1}/{0}.cshtml",
                                            "~/Views/{1}/{0}.vbhtml",
                                            "~/Views/Shared/{0}.cshtml",
                                            "~/Views/Shared/{0}.vbhtml",

                                            //Admin
                                            "~/Administration/Views/{1}/{0}.cshtml",
                                            "~/Administration/Views/{1}/{0}.vbhtml",
                                            "~/Administration/Views/Shared/{0}.cshtml",
                                            "~/Administration/Views/Shared/{0}.vbhtml",
                                      };

            PartialViewLocationFormats = new[]
                                             {
                                                 //themes
                                                "~/Themes/{2}/Views/{1}/{0}.cshtml",
                                                "~/Themes/{2}/Views/{1}/{0}.vbhtml",
                                                "~/Themes/{2}/Views/Shared/{0}.cshtml",
                                                "~/Themes/{2}/Views/Shared/{0}.vbhtml",

                                                //default
                                                "~/Views/{1}/{0}.cshtml",
                                                "~/Views/{1}/{0}.vbhtml",
                                                "~/Views/Shared/{0}.cshtml",
                                                "~/Views/Shared/{0}.vbhtml",

                                                //Admin
                                                "~/Administration/Views/{1}/{0}.cshtml",
                                                "~/Administration/Views/{1}/{0}.vbhtml",
                                                "~/Administration/Views/Shared/{0}.cshtml",
                                                "~/Administration/Views/Shared/{0}.vbhtml",
                                             };




Andrei or the rest of the nop dev team: Is this how your team would solve this issue and would my change create side-effects elsewhere in the code?

I ran all of the unit tests and besides the 1 test always failing (even in fresh downloads) all the tests pass.
12 years ago
skyler.severns wrote:
Is this how your team would solve this issue and would my change create side-effects elsewhere in the code?
I ran all of the unit tests and besides the 1 test always failing (even in fresh downloads) all the tests pass.

I don't know. It needs some testing. Unfortunately, unit tests don't cover a lot of UI stuff. I've just created a work item (investigate it a bit later)
12 years ago
a.m. wrote:
Is this how your team would solve this issue and would my change create side-effects elsewhere in the code?
I ran all of the unit tests and besides the 1 test always failing (even in fresh downloads) all the tests pass.
I don't know. It needs some testing. Unfortunately, unit tests don't cover a lot of UI stuff. I've just created a work item (investigate it a bit later)


In the mean time I'll run my site this way and If I notice anything I'll let you know.
12 years ago
skyler.severns wrote:
In the mean time I'll run my site this way and If I notice anything I'll let you know.

Hi Skyler,

Have you noticed something wrong with this implementation? Does it work fine?
12 years ago
a.m. wrote:
In the mean time I'll run my site this way and If I notice anything I'll let you know.
Hi Skyler,

Have you noticed something wrong with this implementation? Does it work fine?


Andrei thanks for the follow-up. I have yet to try it in 2.1 ( I plan to upgrade once I complete my current task), but in 2.0 I haven't seen any issues.
12 years ago
Hi Skyler,
I have followed your instructions, but I am getting the following error when I try to load the admin page (not even my new plugin, just the main admin page.  I'm assuming that it is failing when  trying to initialize the plugins).

Here is the error:
Server Error in '/' Application.
--------------------------------------------------------------------------------

No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:


[MissingMethodException: No parameterless constructor defined for this object.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +69
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +67

[InvalidOperationException: An error occurred when trying to create a controller of type 'Nop.Admin.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +182
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +196
   System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +49
   System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +13
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +124
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +98
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8920324
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.225
12 years ago
ScottMc101 wrote:
Hi Skyler,
I have followed your instructions, but I am getting the following error when I try to load the admin page (not even my new plugin, just the main admin page.  I'm assuming that it is failing when  trying to initialize the plugins).

Here is the error:
Server Error in '/' Application.
--------------------------------------------------------------------------------

No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:


[MissingMethodException: No parameterless constructor defined for this object.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +98
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +241
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +69
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +67

[InvalidOperationException: An error occurred when trying to create a controller of type 'Nop.Admin.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +182
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +196
   System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +49
   System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +13
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +124
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +98
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8920324
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.225


Hey Scott it sounds like you may have added some dependencies on the constructor but they are not mapped in a dependency registrar class. Would you be able to publish your constructor code?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.