Problem using razor displaytemplates with other basePageType

May 12, 2011 at 1:53 PM
Edited May 13, 2011 at 7:45 AM

Using the NuGet package 3.0.0.2 the DisplayTemplates are installed in the correct folder, but i have the problem that only the ascx templates are used. Looks like it has to do with using a different pageBaseType. It is working correct when using @inherits in view.

Same behavior when clearing the ViewEngines and only adding the RazorViewEngine (for performance reasons).

The error is triggered by @Html.MvcSiteMap().Menu(), and web.config contains the necessary namespaces in the pages section.

 

System.Web.HttpCompileException was unhandled by user code
  Message=c:\kanweg\MvcApplication5\MvcApplication5\Views\Shared\DisplayTemplates\MenuHelperModel.cshtml(6): error CS1061: 'object' does not contain a definition for 'Nodes' and no extension method 'Nodes' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
  Source=System.Web
  ErrorCode=-2147467259
  WebEventCode=0
  SourceCode=#pragma checksum "c:\kanweg\MvcApplication5\MvcApplication5\Views\Shared\DisplayTemplates\MenuHelperModel.cshtml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "E1DDC6FF060A181F86C70AEEBE00F7CA"
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.225
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace ASP {
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Helpers;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.WebPages;
    using System.Web.Mvc;
    using System.Web.Mvc.Ajax;
   
    #line 2 "c:\kanweg\MvcApplication5\MvcApplication5\Views\Shared\DisplayTemplates\MenuHelperModel.cshtml"
    using System.Web.Mvc.Html;
   
    #line default
    #line hidden
    using System.Web.Routing;
    using MvcSiteMapProvider.Web.Html;
   
    #line 3 "c:\kanweg\MvcApplication5\MvcApplication5\Views\Shared\DisplayTemplates\MenuHelperModel.cshtml"
    using MvcSiteMapProvider.Web.Html.Models;
   
    #line default
    #line hidden
   
   
    public class _Page_Views_Shared_DisplayTemplates_MenuHelperModel_cshtml : MvcApplication5.MasterWebViewPage<MvcSiteMapProvider.Web.Html.Models.MenuHelperModel> {
       
#line hidden

       
        public _Page_Views_Shared_DisplayTemplates_MenuHelperModel_cshtml() {
        }
       
        protected ASP.global_asax ApplicationInstance {
            get {
                return ((ASP.global_asax)(Context.ApplicationInstance));
            }
        }
       
        public override void Execute() {



WriteLiteral("\r\n<ul class=\"nav_main\">\r\n");


           
            #line 6 "c:\kanweg\MvcApplication5\MvcApplication5\Views\Shared\DisplayTemplates\MenuHelperModel.cshtml"
     foreach (var node in Model.Nodes) {

           
            #line default
            #line hidden
WriteLiteral("        <li>");


           
            #line 7 "c:\kanweg\MvcApplication5\MvcApplication5\Views\Shared\DisplayTemplates\MenuHelperModel.cshtml"
       Write(Html.DisplayFor(m => node));

           
            #line default
            #line hidden
WriteLiteral(" \r\n");


           
            #line 8 "c:\kanweg\MvcApplication5\MvcApplication5\Views\Shared\DisplayTemplates\MenuHelperModel.cshtml"
             if (node.Children.Any()) {
               
           
            #line default
            #line hidden
           
            #line 9 "c:\kanweg\MvcApplication5\MvcApplication5\Views\Shared\DisplayTemplates\MenuHelperModel.cshtml"
           Write(Html.DisplayFor(m => node.Children));

           
            #line default
            #line hidden
           
            #line 9 "c:\kanweg\MvcApplication5\MvcApplication5\Views\Shared\DisplayTemplates\MenuHelperModel.cshtml"
                                                   
            }

           
            #line default
            #line hidden
WriteLiteral("        </li>\r\n");


           
            #line 12 "c:\kanweg\MvcApplication5\MvcApplication5\Views\Shared\DisplayTemplates\MenuHelperModel.cshtml"
    }

           
            #line default
            #line hidden
WriteLiteral("</ul>");


        }
    }
}

  StackTrace:
       at System.Web.Compilation.AssemblyBuilder.Compile()
       at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
       at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)
       at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
       at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
       at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
       at System.Web.Compilation.BuildManager.GetObjectFactory(String virtualPath, Boolean throwIfNotFound)
       at System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.FileExists(String virtualPath)
       at System.Web.Mvc.BuildManagerViewEngine.FileExists(ControllerContext controllerContext, String virtualPath)
       at System.Web.Mvc.VirtualPathProviderViewEngine.GetPathFromGeneralName(ControllerContext controllerContext, List`1 locations, String name, String controllerName, String areaName, String cacheKey, String[]& searchedLocations)
       at System.Web.Mvc.VirtualPathProviderViewEngine.GetPath(ControllerContext controllerContext, String[] locations, String[] areaLocations, String locationsPropertyName, String name, String controllerName, String cacheKeyPrefix, Boolean useCache, String[]& searchedLocations)
       at System.Web.Mvc.VirtualPathProviderViewEngine.FindPartialView(ControllerContext controllerContext, String partialViewName, Boolean useCache)
       at System.Web.Mvc.ViewEngineCollection.<>c__DisplayClass8.<FindPartialView>b__7(IViewEngine e)
       at System.Web.Mvc.ViewEngineCollection.Find(Func`2 lookup, Boolean trackSearchedPaths)
       at System.Web.Mvc.ViewEngineCollection.FindPartialView(ControllerContext controllerContext, String partialViewName)
       at System.Web.Mvc.Html.TemplateHelpers.ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, String templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions)
       at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData, ExecuteTemplateDelegate executeTemplate)
       at System.Web.Mvc.Html.TemplateHelpers.TemplateHelper(HtmlHelper html, ModelMetadata metadata, String htmlFieldName, String templateName, DataBoundControlMode mode, Object additionalViewData)
       at System.Web.Mvc.Html.TemplateHelpers.TemplateFor[TContainer,TValue](HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData, TemplateHelperDelegate templateHelper)
       at System.Web.Mvc.Html.TemplateHelpers.TemplateFor[TContainer,TValue](HtmlHelper`1 html, Expression`1 expression, String templateName, String htmlFieldName, DataBoundControlMode mode, Object additionalViewData)
       at System.Web.Mvc.Html.DisplayExtensions.DisplayFor[TModel,TValue](HtmlHelper`1 html, Expression`1 expression, String templateName)
       at MvcSiteMapProvider.Web.Html.MenuHelper.Menu(MvcSiteMapHtmlHelper helper, String templateName, SiteMapNode startingNode, Boolean startingNodeInChildLevel, Boolean showStartingNode, Int32 maxDepth, Boolean drillDownToCurrent) in c:\Projects\Codeplex\TFS10\mvcsitemap\Branches\3.0.0\src\MvcSiteMapProvider\MvcSiteMapProvider\Web\Html\MenuHelper.cs:line 238
       at MvcSiteMapProvider.Web.Html.MenuHelper.Menu(MvcSiteMapHtmlHelper helper, SiteMapNode startingNode, Boolean startingNodeInChildLevel, Boolean showStartingNode, Int32 maxDepth, Boolean drillDownToContent) in c:\Projects\Codeplex\TFS10\mvcsitemap\Branches\3.0.0\src\MvcSiteMapProvider\MvcSiteMapProvider\Web\Html\MenuHelper.cs:line 127
       at MvcSiteMapProvider.Web.Html.MenuHelper.Menu(MvcSiteMapHtmlHelper helper) in c:\Projects\Codeplex\TFS10\mvcsitemap\Branches\3.0.0\src\MvcSiteMapProvider\MvcSiteMapProvider\Web\Html\MenuHelper.cs:line 34
       at ASP._Page_Views_Shared__Layout_cshtml.Execute() in c:\kanweg\MvcApplication5\MvcApplication5\Views\Shared\_Layout.cshtml:line 19
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
       at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.WebPages.WebPageBase.<>c__DisplayClass7.<RenderPageCore>b__6(TextWriter writer)
       at System.Web.WebPages.HelperResult.WriteTo(TextWriter writer)
       at System.Web.WebPages.WebPageBase.Write(HelperResult result)
       at System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body)
       at System.Web.WebPages.WebPageBase.PopContext()
       at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
       at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
       at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
       at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
  InnerException:

Coordinator
May 16, 2011 at 9:09 AM


In the Web.config file under the Views folder, add the following under the <system.web.webPages.razor> element:

<system.web.webPages.razor>
  <! -- ... -->
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <! -- ... --> 
      <add namespace="MvcSiteMapProvider.Web.Html" /> 
      <add namespace="MvcSiteMapProvider.Web.Html.Models" /> 
    </namespaces>
  </pages>
</system.web.webPages.razor>
May 16, 2011 at 10:09 AM
Edited May 16, 2011 at 10:13 AM
maartenba wrote:


In the Web.config file under the Views folder, add the following under the <system.web.webPages.razor> element:

<system.web.webPages.razor>
  <! -- ... -->
  <pages pageBaseType="MvcApplication.MyBasePage">
    <namespaces>
      <! -- ... --> 
      <add namespace="MvcSiteMapProvider.Web.Html" /> 
      <add namespace="MvcSiteMapProvider.Web.Html.Models" /> 
    </namespaces>
  </pages>
</system.web.webPages.razor>

I am using a different pagebase for common functionality. It is working correctly when I use the default pagebase.

May 17, 2011 at 10:12 AM

Instead of using a different basePage i'm now using a ViewModelBase for common functionality (with global action attribute), so I can use this SiteMapProvider with default pageBase.

May 30, 2011 at 6:28 PM

This issue remains open because I too am experiencing it.

My .cshtml view pages inherit my custom ViewPage like this:

@inherits myweb.Web.Extensibility.CustomViewPage<dynamic>

MVC SiteMapProvider only picks up the .ascx files from DisplayTemplates folder.

Aug 2, 2011 at 12:29 PM
markoh wrote:

This issue remains open because I too am experiencing it.

My .cshtml view pages inherit my custom ViewPage like this:

@inherits myweb.Web.Extensibility.CustomViewPage<dynamic>

MVC SiteMapProvider only picks up the .ascx files from DisplayTemplates folder.

 

MvcSiteMapProvider will use the ASCX file if it is there.  Rename or delete the ASCX file and notice that the provider uses the razor (CSHTML) file.