<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>controller Archives - Blog IT</title>
	<atom:link href="https://blogit.create.pt/tag/controller/feed/" rel="self" type="application/rss+xml" />
	<link>https://blogit.create.pt/tag/controller/</link>
	<description>Create IT blogger community</description>
	<lastBuildDate>Thu, 10 Jan 2019 12:46:25 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>Umbraco and Donut Output Cache</title>
		<link>https://blogit.create.pt/andresantos/2016/06/30/umbraco-and-donut-output-cache/</link>
					<comments>https://blogit.create.pt/andresantos/2016/06/30/umbraco-and-donut-output-cache/#respond</comments>
		
		<dc:creator><![CDATA[André Santos]]></dc:creator>
		<pubDate>Thu, 30 Jun 2016 14:22:01 +0000</pubDate>
				<category><![CDATA[Umbraco]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Devtrends]]></category>
		<category><![CDATA[Donut Output Cache]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[mvc 5]]></category>
		<category><![CDATA[MVCDonutCaching]]></category>
		<category><![CDATA[performance]]></category>
		<guid isPermaLink="false">http://blogit.create.pt/andresantos/?p=1571</guid>

					<description><![CDATA[<p>Donut Output Caching is a type of output caching where certain parts of a web page are not cached. It&#8217;s a simple way of boosting your site performance! ASP.NET doesn&#8217;t provide a native way of donut output caching, so we must resort to a great NuGet package called MVCDonutCaching. You can read all about it here: http://www.devtrends.co.uk/blog/donut-output-caching-in-asp.net-mvc-3. [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/andresantos/2016/06/30/umbraco-and-donut-output-cache/">Umbraco and Donut Output Cache</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Donut Output Caching is a type of output caching where certain parts of a web page are not cached. It&#8217;s a simple way of boosting your site performance!</p>
<p>ASP.NET doesn&#8217;t provide a native way of donut output caching, so we must resort to a great NuGet package called <a href="https://github.com/moonpyk/mvcdonutcaching">MVCDonutCaching</a>. You can read all about it here: <a href="http://www.devtrends.co.uk/blog/donut-output-caching-in-asp.net-mvc-3">http://www.devtrends.co.uk/blog/donut-output-caching-in-asp.net-mvc-3</a>.</p>
<p>Since Umbraco is built on top of ASP.NET MVC, we can easily use this package on our websites. There are, however, some caveats, because Umbraco doesn&#8217;t use the native MVC routing.</p>
<p>For Umbraco, every page that uses the same document type is processed through the same route, therefore, if we have 100 news articles that use the NewsItem document type, from the moment we open one news article page, every other news item page will display the same information, despite having a different URL!</p>
<p><span id="more-6482"></span></p>
<p>The way to bypass this issue is the following:</p>
<p>Create a Global.cs file that includes the logic to cache a page based on their url and not the document type</p>
<pre class="brush: csharp; title: Global.cs; notranslate">
namespace CreateIT.Web
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Umbraco.Web;

    public class Global : UmbracoApplication
    {
        public override string GetVaryByCustomString(HttpContext context, string custom)
        {
            if (custom.ToLower() == &quot;url&quot;)
            {
                return &quot;url=&quot; + context.Request.Url.AbsoluteUri;
            }

            return base.GetVaryByCustomString(context, custom);
        }
    }
}
</pre>
<p>Then, instruct Umbraco to use this class when launching the UmbracoApplication by changing the Global.asax file:</p>
<pre class="brush: plain; title: Global.asax; notranslate">
&amp;lt;%@ Application Codebehind=&quot;Global.cs&quot; Inherits=&quot;CreateIT.Web.Global&quot; Language=&quot;C#&quot; %&amp;gt;
</pre>
<p>Finally, setup an Output Cache profile in your Web.config:</p>
<pre class="brush: xml; title: Web.config; notranslate">
&amp;lt;system.web&amp;gt;
    (...)
    &amp;lt;caching&amp;gt;
      &amp;lt;outputCacheSettings&amp;gt;
        &amp;lt;outputCacheProfiles&amp;gt;
          &amp;lt;add enabled=&quot;true&quot; name=&quot;CreateIT.Actions&quot; duration=&quot;900&quot; location=&quot;Server&quot; varyByCustom=&quot;url&quot; varyByParam=&quot;*&quot; /&amp;gt;
        &amp;lt;/outputCacheProfiles&amp;gt;
      &amp;lt;/outputCacheSettings&amp;gt;
    &amp;lt;/caching&amp;gt;
&amp;lt;/system.web&amp;gt;
</pre>
<p>And decorate the chosen controllers (that you used to <a href="https://our.umbraco.org/documentation/reference/routing/custom-controllers">hijack the Umbraco routes</a>) with the Donut Output Cache attribute:</p>
<pre class="brush: csharp; title: HomepageController.cs; notranslate">
&#x5B;DonutOutputCache(CacheProfile = &quot;CreateIT.Actions&quot;)]
public class HomepageController : Umbraco.Web.Mvc.RenderMvcController
{
</pre>
<p>If you wish to exclude a certain part of your page from the cache, just create a Surface Controller action that returns a partial view and use the extension method provided by MVCDonutCaching for @Html.Action().</p>
<p>The post <a href="https://blogit.create.pt/andresantos/2016/06/30/umbraco-and-donut-output-cache/">Umbraco and Donut Output Cache</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/andresantos/2016/06/30/umbraco-and-donut-output-cache/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Pre-set common viewmodel properties before action result</title>
		<link>https://blogit.create.pt/andresantos/2014/11/07/pre-set-common-viewmodel-preperties-before-action-result/</link>
					<comments>https://blogit.create.pt/andresantos/2014/11/07/pre-set-common-viewmodel-preperties-before-action-result/#respond</comments>
		
		<dc:creator><![CDATA[André Santos]]></dc:creator>
		<pubDate>Fri, 07 Nov 2014 15:41:17 +0000</pubDate>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[action filter]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[filter provider]]></category>
		<category><![CDATA[global filters]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[mvc 5]]></category>
		<category><![CDATA[viewmodel]]></category>
		<guid isPermaLink="false">http://blogit.create.pt/andresantos/?p=121</guid>

					<description><![CDATA[<p>So, I defined a master model that is shared by every view in my MVC site. This model defines a bunch of properties that are always used on every page, such as a page title, page description, the google tag manager code, etc. I need this model to be filled before the action result, so that [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/andresantos/2014/11/07/pre-set-common-viewmodel-preperties-before-action-result/">Pre-set common viewmodel properties before action result</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>So, I defined a master model that is shared by every view in my MVC site. This model defines a bunch of properties that are always used on every page, such as a page title, page description, the google tag manager code, etc.</p>
<p>I need this model to be filled before the action result, so that I can use those properties on the view.</p>
<p>In order to do this, I created a custom Action Filter Attribute that is used by every action:</p>
<pre class="brush: csharp; title: SetupMasterModelActionFilter.cs; notranslate">

    public class SetupMasterModelActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            ViewResultBase result = filterContext.Result as ViewResultBase;
            if (result == null)
            {
                return;
            }

            MasterModel model = result.Model as MasterModel;
            if (model == null)
            {
                return;
            }

            SetupMasterModel(model);
            SetupViewBag(result, model);
        }

        private void SetupMasterModel(MasterModel model)
        {
            ...
        }

        private void SetupViewBag(ViewResultBase result, MasterModel model)
        {
            ...
        }
   }
</pre>
<p><span id="more-6463"></span></p>
<p>In order to execute this filter before every action I had to register it in the GlobalFilters at the Global.asax.cs file:</p>
<pre class="brush: csharp; title: Global.asax.cs; notranslate">

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            GlobalFilters.Filters.Add(new SetupMasterModelActionFilter());

            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
</pre>
<p>There are, however, some pages that don&#8217;t use this master model, so I need to exclude the use of this filter on certain actions. In order to do this, I created a new Filter Provider that looks for an exclude filter and filters out the one targeted:</p>
<pre class="brush: csharp; title: ExcludeFilterProvider.cs; notranslate">

    public class ExcludeFilterProvider : IFilterProvider
    {
        private FilterProviderCollection filterProviders;

        public ExcludeFilterProvider(IFilterProvider&#x5B;] filters)
        {
            this.filterProviders = new FilterProviderCollection(filters);
        }

        public IEnumerable&lt;Filter&gt; GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            Filter&#x5B;] filters = this.filterProviders.GetFilters(controllerContext, actionDescriptor).ToArray();

            IEnumerable&lt;ExcludeFilterAttribute&gt; excludeFilters = (from f in filters where f.Instance is ExcludeFilterAttribute select f.Instance as ExcludeFilterAttribute);

            List&lt;Type&gt; filterTypesToRemove = new List&lt;Type&gt;();
            foreach (ExcludeFilterAttribute excludeFilter in excludeFilters)
            {
                filterTypesToRemove.Add(excludeFilter.FilterType);
            }

            IEnumerable&lt;Filter&gt; res = (from filter in filters where !filterTypesToRemove.Contains(filter.Instance.GetType()) select filter);
            return res;
        }
    }
</pre>
<p>The exclude filter attribute that will decorate the excluded actions:</p>
<pre class="brush: csharp; title: ExcludeFilterAttribute.cs; notranslate">

    public class ExcludeFilterAttribute : FilterAttribute
    {
        private Type filterType;

        public ExcludeFilterAttribute(Type filterType)
        {
            this.filterType = filterType;
        }

        public Type FilterType
        {
            get
            {
                return this.filterType;
            }
        }
    } 

</pre>
<p>And we add the new provider at the start of the application and remove the old ones:</p>
<pre class="brush: csharp; title: Global.asax.cs; notranslate">

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            GlobalFilters.Filters.Add(new SetupMasterModelActionFilter());

            IFilterProvider&#x5B;] providers = FilterProviders.Providers.ToArray();
            FilterProviders.Providers.Clear();
            FilterProviders.Providers.Add(new ExcludeFilterProvider(providers));

            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }

</pre>
<p>With this, every action will setup the master model properties except the ones that implement the exclude filter:</p>
<pre class="brush: csharp; title: HomeController.cs; notranslate">

    public class HomeController : Controller
    {
        &#x5B;ExcludeFilter(typeof(SetupMasterModelActionFilter))]
        public ActionResult Index()
        {
            HomeModel model = new HomeModel();

            return View(model);
        }
    }

</pre>
<p>The post <a href="https://blogit.create.pt/andresantos/2014/11/07/pre-set-common-viewmodel-preperties-before-action-result/">Pre-set common viewmodel properties before action result</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/andresantos/2014/11/07/pre-set-common-viewmodel-preperties-before-action-result/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
