Umbraco Cannot render a macro when there is no current PublishedContentRequest

Hope you can help me.I encountered that error upon calling a view from my umbraco razor page using RenderMvcController. It's seems it can't render my created macro inside a layout page.

Line 4: } Line 5:
Line 6: @Umbraco.RenderMacro("ExternalRedirect") Line 7:
Line 8:

Source File: c:WorkingGitIDUMB PackagesWebsiteViewsDefault.cshtml Line: 6

Stack Trace:

[InvalidOperationException: Cannot render a macro when there is no current PublishedContentRequest.]
Umbraco.Web.UmbracoHelper.RenderMacro(String alias, IDictionary 2 parameters) +142 ASP._Page_Views_Default_cshtml.Execute() in c:WorkingGitIDUMB PackagesWebsiteViewsDefault.cshtml:6
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +125
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +196
System.Web.WebPages.WebPageBase.Write(HelperResult result) +89
System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action
2 parameters) +142 ASP._Page_Views_Default_cshtml.Execute() in c:WorkingGitIDUMB PackagesWebsiteViewsDefault.cshtml:6
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +125
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +196
System.Web.WebPages.WebPageBase.Write(HelperResult result) +89
System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action
2 parameters) +142 ASP._Page_Views_Default_cshtml.Execute() in c:WorkingGitIDUMB PackagesWebsiteViewsDefault.cshtml:6
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +125
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +196
System.Web.WebPages.WebPageBase.Write(HelperResult result) +89
System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action
1 body) +233
System.Web.WebPages.WebPageBase.PopContext() +287
Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer) +140
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +378 System.Web.Mvc.<>c__DisplayClass1a.b__17() +33 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func 1 continuation) +799724 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func 1 continuation) +799724 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +263
System.Web.Mvc.Async.<>c__DisplayClass25.b__22(IAsyncResult asyncResult) +787092
System.Web.Mvc.<>c__DisplayClass1d.b__18(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +15 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +65
System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +15 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +51
System.Web.Mvc.<>c__DisplayClass8.b__3(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +15 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +51
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

public class BlogController : RenderMvcController
{
   public BlogController()
       : this(UmbracoContext.Current)
   {
   }

   public BlogController(UmbracoContext umbracoContext)
       : base(umbracoContext)
   {
   }

   public ActionResult Index(int id)
   {
       return View("12321");
   }

   public ActionResult YearPost(int id)
   {
       string test = "Blog";
       var criteria = ExamineManager.Instance.DefaultSearchProvider.CreateSearchCriteria("content");
       var filterBlogs = id == 0 ? criteria.NodeTypeAlias("Blog") : criteria.NodeName("Blog");
       var result = Umbraco.TypedSearch(filterBlogs.Compile()).ToArray().First();

       var renderModel = CreateRenderModel(result);
       string viewBlog = "BlogPostPage";
       return View(viewBlog, renderModel);
   }
   private RenderModel CreateRenderModel(IPublishedContent content)
   {
       var model = new RenderModel(content,CultureInfo.CurrentUICulture);

       //add an umbraco data token so the umbraco view engine executes
       RouteData.DataTokens["umbraco"] = model;
       RouteData.DataTokens["umbraco-doc-request"] = UmbracoContext.PublishedContentRequest;
       RouteData.DataTokens["umbraco-context"] = UmbracoContext;
       return model;
    }
}
链接地址: http://www.djcxy.com/p/72698.html

上一篇: 子文件夹下的MVC路由

下一篇: Umbraco当没有当前PublishedContentRequest时,无法呈现宏