Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Niklas Raab 9 posts 79 karma points
    Feb 15, 2017 @ 11:53
    Niklas Raab
    0

    InvalidCastException in Macro in Method ExecutePageHierarchy

    Hello,

    i have two nested Macros. The first one is this:

        @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var sliderId = @Model.MacroParameters["Slider"];
        var sliderContent = (Umbraco.Web.PublishedContentModels.Slider)Umbraco.TypedContent(sliderId);
        var teasers = sliderContent.Teasers.Split(',').Select(s => (IMasterTeaser)Umbraco.TypedContent(s));
    }
    <div id="carouselMacro" class="carousel slide" data-ride="carousel">
        <ol class="carousel-indicators">
            <li data-target="#carouselMacro" data-slide-to="0" class="active"></li>
            <li data-target="#carouselMacro" data-slide-to="1"></li>
            <li data-target="#carouselMacro" data-slide-to="2"></li>
        </ol>
    
        <div class="carousel-inner" role="listbox">
            @{var enumerable = teasers as IMasterTeaser[] ?? teasers.ToArray();}
            @foreach (var teaser in enumerable)
            {
                @Umbraco.RenderMacro(teaser.TemplateMacro.ToString(), new { TeaserId = teaser.Id, IsFirst = teaser == enumerable.First() })
            }
        </div>
    
        <a class="left carousel-control" href="#carouselMacro" role="button" data-slide="prev">
            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
            <span class="sr-only">Zurück</span>
        </a>
        <a class="right carousel-control" href="#carouselMacro" role="button" data-slide="next">
            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
            <span class="sr-only">Weiter</span>
        </a>
    </div>
    

    the second Macro ist nested in the first one and looks like this:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var teaser = (Teaser) Umbraco.TypedContent(Model.MacroParameters["TeaserId"]); //Throws exception
    }
    <div class="item @(((bool)Model.MacroParameters["IsFirst"])? "active" : "") ">
    </div>
    

    I marked the postition were the exeption is thrown.

    This is my Stacktrace:

    System.InvalidCastException: Specified cast is not valid. at ASP.PageViewsMacroPartialsDefaultImageTeaserMacrocshtml.Execute() in c:\Dev\PGM\trunk.NET\Orca.Homepage.Web\Views\MacroPartials\DefaultImageTeaserMacro.cshtml:line 5 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.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer) at Umbraco.Web.Mvc.ControllerExtensions.RenderViewResultAsString(ControllerBase controller, ViewResultBase viewResult) at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, IPublishedContent content) at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, INode node) at umbraco.macro.LoadPartialViewMacro(MacroModel macro) at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId) at Umbraco.Web.UmbracoComponentRenderer.RenderMacro(macro m, IDictionary2 parameters, page umbracoPage) at Umbraco.Web.UmbracoComponentRenderer.RenderMacro(String alias, IDictionary2 parameters, page umbracoPage) at Umbraco.Web.UmbracoComponentRenderer.RenderMacro(String alias, IDictionary2 parameters) at Umbraco.Web.UmbracoHelper.RenderMacro(String alias, Object parameters) at ASP._Page_Views_MacroPartials_Slider_cshtml.Execute() in c:\Dev\PGM\trunk\.NET\Orca.Homepage.Web\Views\MacroPartials\Slider.cshtml:line 18 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.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer) at Umbraco.Web.Mvc.ControllerExtensions.RenderViewResultAsString(ControllerBase controller, ViewResultBase viewResult) at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, IPublishedContent content) at Umbraco.Web.Macros.PartialViewMacroEngine.Execute(MacroModel macro, INode node) at umbraco.macro.LoadPartialViewMacro(MacroModel macro) at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId) at Umbraco.Web.UmbracoComponentRenderer.RenderMacro(macro m, IDictionary2 parameters, page umbracoPage) at Umbraco.Web.UmbracoComponentRenderer.RenderMacro(String alias, IDictionary2 parameters, page umbracoPage) at Umbraco.Web.UmbracoComponentRenderer.RenderMacro(String alias, IDictionary2 parameters) at Umbraco.Web.UmbracoHelper.RenderMacro(String alias, IDictionary`2 parameters) at ASP.PageViewsPartialsgrideditorsmacrocshtml.Execute() in c:\Dev\PGM\trunk.NET\Orca.Homepage.Web\Views\Partials\Grid\Editors\Macro.cshtml:line 13 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.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer) at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection) at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model) at ASP.PageViewsPartialsgrideditorsbase_cshtml.Execute() in c:\Dev\PGM\trunk.NET\Orca.Homepage.Web\Views\Partials\Grid\Editors\Base.cshtml:line 19

    I decompiled the throwing method it looks like this:

    public override void ExecutePageHierarchy()
        {
          if (WebPageHttpHandler.ShouldGenerateSourceHeader(this.Context))
          {
            try
            {
              string virtualPath = this.VirtualPath;
              if (virtualPath != null)
              {
                string str = this.Context.Request.MapPath(virtualPath);
                if (!str.IsEmpty())
                  this.PageContext.SourceFiles.Add(str);
              }
            }
            catch
            {
            }
          }
          TemplateStack.Push(this.Context, (ITemplateFile) this); //Think throwed here
          try
          {
            this.Execute();
          }
          finally
          {
            TemplateStack.Pop(this.Context);
          }
        }
    

    For my opinion it only can throw the exeption on the marked line but "this" is implementing "ITemplateFile".

    The error occured on a fresh umbraco installation.

    Anyone some ideas?

    Thanks a lot,

    Niklas

  • Steven Harland 78 posts 518 karma points c-trib
    Feb 15, 2017 @ 12:47
    Steven Harland
    0

    Hi Niklas,

    It would appear that whatever is returned by Umbraco.TypedContent(Model.MacroParameters["TeaserId"]) is not in fact a Teaser, so cannot be cast as such.

    Is this at all possible with your document type setup?

    Steven.

  • Niklas Raab 9 posts 79 karma points
    Feb 15, 2017 @ 13:23
    Niklas Raab
    0

    Thanks for response!

    This is also not working:

    var teaser = Umbraco.Content(Model.MacroParameters["TeaserId"].ToString());
    

    It throws the same error, don't know why.

    Hope this is not a big problem :(

    Niklas

Please Sign in or register to post replies

Write your reply to:

Draft