<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><language>en</language><title>Blog posts by Dave Beattie</title> <link>https://world.optimizely.com/blogs/dave-beattie/</link><description></description><ttl>60</ttl><generator>Optimizely World</generator><item> <title>Episerver Routing Error ‘The routed data is of type..and the binding context data ... is of type ..., neither of which matches the requested type...’</title>            <link>https://world.optimizely.com/blogs/dave-beattie/dates/2021/3/episerver-routing-error-the-routed-data-is-of-type--and-the-binding-context-data-----is-of-type-----neither-of-which-matches-the-requested-type/</link>            <description>&lt;p&gt;Recently while trying to build an Episerver ecommerce solution I ran into an error with a type mismatch where navigating to any type of node or product failed with a type mismatch.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/link/9954ac408fe5443b9d2aa8551c2b24cc.aspx&quot; /&gt;&lt;/p&gt;
&lt;p&gt;The &amp;lsquo;requested type&amp;rsquo; for each product was always the same class.&amp;nbsp; I then noticed whenever a new type of product was added, the requested type was always the first type of model alphabetically.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/link/447777f8cacb4b39bf214443c4f646d8.aspx&quot; /&gt;&lt;/p&gt;
&lt;p&gt;The controller for each node and product type was inheriting from a base controller.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/link/ecb34914d33d47aebe82724af052799d.aspx&quot; /&gt;&lt;/p&gt;
&lt;p&gt;This turned out to be where the problem lay.&amp;nbsp; Each controller looked ok and seemed to be configured for the relevant model.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/link/a366c9ee6eae44fa85c865ad74b16f5c.aspx&quot; /&gt;&lt;/p&gt;
&lt;p&gt;However, there was a typo in the inheritance on the base class here, which led to each controller compiling as ContentController&amp;lt;CatalogContentBase&amp;gt;.&amp;nbsp; Thus when processing the route, episerver was getting confused as to which model it should be resolving the route for, and just took a stab in the dark and grabbed the first model inheriting from CatalogContentBase it could find.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/link/56905263db214b46b3da8583d3dff4bb.aspx&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Changing this to a type parameter constraint, so any class implementing this controller would have to be routing a model implementing catalogContentBase, fixed the error.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/link/c69bd9be25634162bb618603c3045a6b.aspx&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Essentially if you run into this error, check that all your controllers are routing the correct content type, and if there is a base class, ensure this is also configured properly.&lt;/p&gt;</description>            <guid>https://world.optimizely.com/blogs/dave-beattie/dates/2021/3/episerver-routing-error-the-routed-data-is-of-type--and-the-binding-context-data-----is-of-type-----neither-of-which-matches-the-requested-type/</guid>            <pubDate>Tue, 16 Mar 2021 09:32:24 GMT</pubDate>           <category>Blog post</category></item><item> <title>Episerver AJAX and standard MVC calls with Localization</title>            <link>https://world.optimizely.com/blogs/dave-beattie/dates/2021/3/episerver-ajax-and-standard-mvc-calls-with-localization/</link>            <description>&lt;p&gt;This post is inspired by Stefan&amp;rsquo;s solution &lt;a href=&quot;https://stefanolsen.com/posts/ensuring-the-right-ajax-content-language-using-a-filter-attribute/&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Recently I was involved in a project replacing a custom translation solution on a client&amp;rsquo;s site with a more standard Episerver solution using Episerver Languages to translate content and DblocalizationProvider for static text.&lt;/p&gt;
&lt;p&gt;One issue we ran into was with API calls and AJAX requests.&amp;nbsp; The site is an ecommerce site and there were several places where requests where directed through a standard mvc controller/action route, rather than going through Episerver content routing.&amp;nbsp; This proved problematic when attempting to return translated content such as error messages as we found the current culture was lost and the site was using the default culture.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;We settled on using an ActionFilter to retrieve the language code from the url.&amp;nbsp;&amp;nbsp; Initially we had used Stefan&amp;rsquo;s implementation tailored to AJAX calls.&amp;nbsp; However, we ran into further issues as a payment gateway implementation was using an api method as it&amp;rsquo;s return url in order to process the payment status and either complete the order or return an error message.&amp;nbsp; This we created something that would retrieve the language code from any request.&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt; public class StandardMvcRequestSetLanguageAttribute : ActionFilterAttribute
   {

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext?.RequestContext?.HttpContext.Request == null)
            {
                return;
            }

            HttpRequestBase httpRequest = filterContext.RequestContext.HttpContext.Request;

            if (filterContext.RequestContext.RouteData.Values[&quot;language&quot;] != null)
            {
                var lang = filterContext.RequestContext.RouteData.Values[&quot;language&quot;].ToString();

                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
            }         
        }
    }

  &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&quot;/link/f96f9671b4714468aa9a19b31858ce1f.aspx&quot; /&gt;&lt;/p&gt;
&lt;p&gt;We then discovered that Episerver wasn&amp;rsquo;t supported localized urls for non content paths (it&amp;rsquo;s perfectly fine for links to content that goes through Episerver&amp;rsquo;s routing.)&amp;nbsp; Therefore we also registered a basic path with the language code in.&amp;nbsp; Our ajax calls and the return urls for our payment gateway were now able to return translated error or success messages.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;img src=&quot;/link/bbc823a402cf41519dbdd659ac85c1ff.aspx&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description>            <guid>https://world.optimizely.com/blogs/dave-beattie/dates/2021/3/episerver-ajax-and-standard-mvc-calls-with-localization/</guid>            <pubDate>Tue, 16 Mar 2021 09:32:18 GMT</pubDate>           <category>Blog post</category></item></channel>
</rss>