I'm using an inline Razor macro which attempts to call one of two other Razor macos using renderpage, dependent on checking a cookie:
<umbraco:Macro runat="server" language="cshtml"> @{ HttpCookie membersCookie = new HttpCookie("FSCMembersPreview"); membersCookie = Request.Cookies["FSCMembersPreview"]; // Read the cookie information and display it. if (membersCookie == null) { @RenderPage("~/macroScripts/FSCListOfIndividualsFamiliesCourses.cshtml",Model.category); } else { @RenderPage("~/macroScripts/FSCListOfIndividualsFamiliesCoursesMembersPreview.cshtml",Model.category); } } </umbraco:Macro>
Which works fine, but the two razor scripts being called aren't being cached, & I really really need them to be. Does anyone know of a way of calling a Razor script inline in thie way and it using the cache? Wondering about RenderMacroContentbut can't see how it would work. Thanks!
Hi. It seems that you can add Cache="..." attribute to your <umbraco:Macro> element to set cache duration, but I cannot see a way to have per-member cache for inline scripts. By the way, is there any reason not to simply convert it to a usual named macro? You will have all cache functionality and I beleive you also will improve readability of your template sources.
Thanks Rodion. I'm not clear how I can convert it to a usual named macro given that its execution needs to be conditional on the result of the cookie check. If I'm missing the blindingly obvious, let me know - I'll be delighted!
1) through HttpContext.Current.Request.Cookies collection from anywhere;
2) and, anyway, your Razor code is eventually compiled to the class inheriting from the DynamicNodeContext class, the WebPage class and so on. It doesn't depend on a way you use it - no matter it's an inline script or a macro. The only difference is where your script source comes from - from a separate file or straight from a string on the page. So you can use Request.Cookie[bla-bla-bla] and @RenderPage calls in your macro script as well as you do in your inline script.
renderpage and caching
I'm using an inline Razor macro which attempts to call one of two other Razor macos using renderpage, dependent on checking a cookie:
Which works fine, but the two razor scripts being called aren't being cached, & I really really need them to be. Does anyone know of a way of calling a Razor script inline in thie way and it using the cache? Wondering about RenderMacroContentbut can't see how it would work.
Thanks!
Hi. It seems that you can add Cache="..." attribute to your <umbraco:Macro> element to set cache duration, but I cannot see a way to have per-member cache for inline scripts. By the way, is there any reason not to simply convert it to a usual named macro? You will have all cache functionality and I beleive you also will improve readability of your template sources.
Thanks Rodion. I'm not clear how I can convert it to a usual named macro given that its execution needs to be conditional on the result of the cookie check. If I'm missing the blindingly obvious, let me know - I'll be delighted!
You can access the request cookies:
1) through HttpContext.Current.Request.Cookies collection from anywhere;
2) and, anyway, your Razor code is eventually compiled to the class inheriting from the DynamicNodeContext class, the WebPage class and so on. It doesn't depend on a way you use it - no matter it's an inline script or a macro. The only difference is where your script source comes from - from a separate file or straight from a string on the page. So you can use Request.Cookie[bla-bla-bla] and @RenderPage calls in your macro script as well as you do in your inline script.
is working on a reply...