Error executing template "Designs/Swift/_parsed/Swift_Preferences.parsed.cshtml"
System.NullReferenceException: Object reference not set to an instance of an object.
   at CompiledRazorTemplates.Dynamic.RazorEngine_e656a0a786114ab98db3678126425051.Execute() in D:\dynamicweb.net\Solutions\twodayco3\evasolo.cloud.dynamicweb-cms.com\Files\Templates\Designs\Swift\_parsed\Swift_Preferences.parsed.cshtml:line 212
   at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context, TextWriter reader)
   at RazorEngine.Templating.RazorEngineService.RunCompile(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
   at RazorEngine.Templating.RazorEngineServiceExtensions.<>c__DisplayClass16_0.<RunCompile>b__0(TextWriter writer)
   at RazorEngine.Templating.RazorEngineServiceExtensions.WithWriter(Action`1 withWriter)
   at Dynamicweb.Rendering.RazorTemplateRenderingProvider.Render(Template template)
   at Dynamicweb.Rendering.TemplateRenderingService.Render(Template template)
   at Dynamicweb.Rendering.Template.RenderRazorTemplate()

1 @inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.PageViewModel> 2 @inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.PageViewModel> 3 @using System 4 @using System.Text.RegularExpressions 5 @using System.IO 6 7 8 9 @{ 10 string layout = Model.Item.GetRawValueString("PreferencesLayout", "modal"); 11 layout = Dynamicweb.Context.Current.Request["Layout"] != null ? Dynamicweb.Context.Current.Request["Layout"] : layout; 12 layout = layout == "" ? "modal" : layout; 13 string type = Dynamicweb.Context.Current.Request["Type"] != null ? Dynamicweb.Context.Current.Request["Type"] : "language"; 14 15 string groupId = Dynamicweb.Context.Current.Request["GroupID"] != null ? Dynamicweb.Context.Current.Request["GroupID"].ToString() : ""; 16 string productId = Dynamicweb.Context.Current.Request["ProductID"] != null ? Dynamicweb.Context.Current.Request["ProductID"].ToString() : ""; 17 string variantId = Dynamicweb.Context.Current.Request["VariantID"] != null ? Dynamicweb.Context.Current.Request["VariantID"].ToString() : ""; 18 19 string linkFontSize = Dynamicweb.Context.Current.Request["FontSize"] != null ? Dynamicweb.Context.Current.Request["FontSize"].ToString() : ""; 20 string linkFontWeight = Dynamicweb.Context.Current.Request["FontWeight"] != null ? Dynamicweb.Context.Current.Request["FontWeight"].ToString() : ""; 21 string linkCasing = Dynamicweb.Context.Current.Request["Casing"] != null ? Dynamicweb.Context.Current.Request["Casing"].ToString() : ""; 22 23 Regex reg = new Regex(@"\(([^\)]+)\)"); 24 List<Dynamicweb.Content.Page> languages = new List<Dynamicweb.Content.Page>(); 25 26 int pageId = Dynamicweb.Context.Current.Request["CurrentPageID"] != null ? Convert.ToInt32(Dynamicweb.Context.Current.Request["CurrentPageID"]) : Pageview.ID; 27 var currentPage = Dynamicweb.Services.Pages.GetPage(pageId); //Get the correct page instance 28 29 if (currentPage.Area.IsMaster) { 30 languages.Add(currentPage); 31 if (currentPage.Languages != null) { 32 foreach (var language in currentPage.Languages) { 33 if (language.Area.Active == true) { 34 languages.Add(language); 35 } 36 } 37 } 38 } else { 39 languages.Add(currentPage.MasterPage); 40 if (currentPage.MasterPage != null) { 41 if (currentPage.MasterPage.Languages != null) { 42 foreach (var language in currentPage.MasterPage.Languages) { 43 if (language.Area.Active == true) { 44 languages.Add(language); 45 } 46 } 47 } 48 } 49 } 50 51 languages = languages.Count > 1 ? languages.OrderBy(language => language.Area.CultureInfo.DisplayName).ToList() : languages; 52 53 string siteLanguage = Pageview.Area.CultureInfo.Name; 54 Uri url = Dynamicweb.Context.Current.Request.Url; 55 string hostName = url.Host; // domain.com/da-dk or domain.com/en-us 56 57 var ecomCountries = Dynamicweb.Ecommerce.Services.Countries.GetCountries() != null ? Dynamicweb.Ecommerce.Services.Countries.GetCountries().OrderBy(country => country.Name) : null; 58 var currencies = Dynamicweb.Ecommerce.Services.Currencies.GetAllCurrencies() != null ? Dynamicweb.Ecommerce.Services.Currencies.GetAllCurrencies().OrderBy(currency => currency.Code) : null; 59 60 string modalTheme = !string.IsNullOrWhiteSpace(Model.Item.GetRawValueString("ModalTheme")) ? " theme " + Model.Item.GetRawValueString("ModalTheme").Replace(" ", "").Trim().ToLower() : ""; 61 } 62 63 64 @* Modal *@ 65 66 @if (layout == "modal") 67 { 68 int updatePageId = pageId; 69 70 <div class="modal-content @modalTheme item_@Model.Item.SystemName.ToLower()"> 71 <div class="modal-header"> 72 <h5 class="modal-title" id="PreferencesModalLabel">@Translate("Preferences")</h5> 73 <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> 74 </div> 75 <div class="modal-body d-grid gap-3" id="PreferencesModalBody"> 76 <input type="hidden" name="CurrentPageID" value="@pageId" /> 77 @if (groupId != "") { 78 <input type="hidden" name="GroupID" value="@groupId" /> 79 } 80 81 @if (productId != "") { 82 <input type="hidden" name="ProductID" value="@productId" /> 83 } 84 85 @if (variantId != "") { 86 <input type="hidden" name="VariantID" value="@variantId" /> 87 } 88 89 90 @if (languages.Count() > 1 && Model.Item.GetBoolean("LanguageSelector")) 91 { 92 <form action="/Default.aspx" data-response-target-element="PreferencesModalContent" data-layout-template="Swift_Preferences.cshtml" data-preloader="inline"> 93 <input type="hidden" name="Layout" value="modal" /> 94 <input type="hidden" name="CurrentPageID" value="@pageId" /> 95 <input type="hidden" name="GroupID" value="@groupId" /> 96 <input type="hidden" name="ProductID" value="@productId" /> 97 <input type="hidden" name="VariantID" value="@variantId" /> 98 99 <div class="form-floating"> 100 <select id="LanguageSelect" name="ID" class="form-select d-col-12" aria-label="@Translate("Select language")" onchange="swift.PageUpdater.Update(event)"> 101 @foreach (var language in languages) 102 { 103 if (language != null && language.Published && language.Active && language.Area.Active && language.Area.Published) { 104 string name = reg.Replace(language.Area.CultureInfo.DisplayName, ""); 105 string selectedLanguage = language.Area.ID == Pageview.Area.ID ? "selected" : ""; 106 updatePageId = language.Area.ID == Pageview.Area.ID ? language.ID : updatePageId; 107 108 var masterPageId = Pageview.Page?.MasterPageId != null && Pageview.Page.MasterPageId != 0 ? Pageview.Page.MasterPageId : Pageview.ID; 109 var preferencePageId = Dynamicweb.Services.Pages.GetPageIDByMasterID(masterPageId, language.AreaId); 110 preferencePageId = preferencePageId == 0 ? Pageview.Page.MasterPageId : preferencePageId; 111 preferencePageId = preferencePageId == 0 ? Pageview.Page.ID : preferencePageId; 112 113 <option hreflang="@language.Area.CultureInfo.TwoLetterISOLanguageName" value="@preferencePageId" @selectedLanguage>@name</option> 114 } 115 } 116 </select> 117 <label for="LanguageSelect">@Translate("Language")</label> 118 </div> 119 </form> 120 } 121 122 @if (ecomCountries.Count() > 1 && Model.Item.GetBoolean("CountrySelector")) 123 { 124 <form action="/Default.aspx" data-response-target-element="PreferencesModalContent" data-layout-template="Swift_Preferences.cshtml" data-preloader="inline"> 125 <input type="hidden" name="ID" value="@Pageview.ID" /> 126 <input type="hidden" name="CurrentPageID" value="@pageId" /> 127 <input type="hidden" name="GroupID" value="@groupId" /> 128 <input type="hidden" name="ProductID" value="@productId" /> 129 <input type="hidden" name="VariantID" value="@variantId" /> 130 131 <div class="form-floating"> 132 <select name="CountryCode" id="CountryCodeSelect" class="form-select d-col-12" aria-label="@Translate("Select country")" onchange="swift.PageUpdater.Update(event)"> 133 @foreach (var country in ecomCountries) 134 { 135 var selectedCountry = country.Code2 == Dynamicweb.Ecommerce.Common.Context.Country.Code2 ? "selected" : ""; 136 137 if (!string.IsNullOrEmpty(country.Name)) { 138 <option value="@country.Code2" @selectedCountry>@country.Name</option> 139 } 140 } 141 </select> 142 <label for="CountryCodeSelect">@Translate("Delivery country")</label> 143 </div> 144 </form> 145 } 146 147 @if (currencies.Count() > 1 && Model.Item.GetBoolean("CurrencySelector")) 148 { 149 <form action="/Default.aspx" data-response-target-element="PreferencesModalContent" data-layout-template="Swift_Preferences.cshtml" data-preloader="inline"> 150 <input type="hidden" name="ID" value="@Pageview.ID" /> 151 <input type="hidden" name="CurrentPageID" value="@pageId" /> 152 <input type="hidden" name="GroupID" value="@groupId" /> 153 <input type="hidden" name="ProductID" value="@productId" /> 154 <input type="hidden" name="VariantID" value="@variantId" /> 155 156 <div class="form-floating"> 157 <select name="CurrencyCode" id="CurrencySelect" class="form-select d-col-12" aria-label="@Translate("Select currency")" onchange="swift.PageUpdater.Update(event)"> 158 @foreach (var currency in currencies) 159 { 160 string selectedCurrency = currency.Code == Dynamicweb.Ecommerce.Common.Context.Currency.Code ? "selected" : ""; 161 162 if (!string.IsNullOrEmpty(currency.Code)) { 163 <option value="@currency.Code" @selectedCurrency>@currency.Code</option> 164 } 165 } 166 </select> 167 <label for="CurrencySelect">@Translate("Currency")</label> 168 </div> 169 </form> 170 } 171 </div> 172 <div class="modal-footer"> 173 @{ 174 hostName = !string.IsNullOrEmpty(Pageview.Area.DomainLock) ? Pageview.Area.DomainLock : url.Host; 175 string link = $"Default.aspx?ID={updatePageId}"; 176 link += groupId != "" ? $"&GroupID={groupId}" : ""; 177 link += productId != "" ? $"&ProductID={productId}" : ""; 178 link += variantId != "" ? $"&VariantID={variantId}" : ""; 179 var friendlyUrl = $"{url.Scheme}://{hostName}{Dynamicweb.Frontend.SearchEngineFriendlyURLs.GetFriendlyUrl(link)}"; 180 } 181 182 <a href="@friendlyUrl" class="btn btn-primary w-100">@Translate("Update preferences")</a> 183 </div> 184 </div> 185 } 186 187 188 @* Dropdowns *@ 189 190 @if (layout == "dropdown" && type == "country") { 191 foreach (var country in ecomCountries) 192 { 193 var link = $"{url.Scheme}://{hostName}?ID={currentPage.ID}&AreaID={Pageview.Area.ID}&CountryCode={country.Code2}"; 194 link += groupId != "" ? $"&GroupID={groupId}" : ""; 195 link += productId != "" ? $"&ProductID={productId}" : ""; 196 link += variantId != "" ? $"&VariantID={variantId}" : ""; 197 198 if (!string.IsNullOrEmpty(country.Name)) { 199 <li> 200 <a href="@link" class="dropdown-item nav-link-underline @linkFontSize @linkFontWeight @linkCasing">@country.Name</a> 201 </li> 202 } 203 } 204 } 205 206 @if (layout == "dropdown" && type == "language") { 207 string iconFolder = "/Files/FlagIcons/"; 208 int languagesCount = 0; 209 210 foreach (var language in languages) 211 { 212 if (language.Area != null) { 213 if (language != null && language.Published && language.Active && language.Area.Active && language.Area.Published && language.Area.ID != Dynamicweb.Frontend.PageView.Current().AreaID) { 214 hostName = !string.IsNullOrEmpty(language.Area.DomainLock) ? language.Area.DomainLock : url.Host; 215 string link = $"Default.aspx?ID={language.ID}"; 216 link += groupId != "" ? $"&GroupID={groupId}" : ""; 217 link += productId != "" ? $"&ProductID={productId}" : ""; 218 link += variantId != "" ? $"&VariantID={variantId}" : ""; 219 220 var friendlyUrl = $"{url.Scheme}://{hostName}{Dynamicweb.Frontend.SearchEngineFriendlyURLs.GetFriendlyUrl(link)}"; 221 222 string icon = iconFolder + language.Area.CultureInfo.Name.ToLower() + ".svg"; 223 string name = reg.Replace(language.Area.CultureInfo.DisplayName, ""); 224 225 <li> 226 <a hreflang="@language.Area.CultureInfo.TwoLetterISOLanguageName" href="@friendlyUrl" class="dropdown-item nav-link-underline @linkFontSize @linkFontWeight @linkCasing">@RenderIcon(icon, "icon-2") <span class="align-middle">@name</span></a> 227 </li> 228 229 languagesCount++; 230 } 231 } 232 } 233 234 if (languagesCount == 0) { 235 <li> 236 <span class="dropdown-item fs-7 fw-normal"> 237 @Translate("Page is not translated") 238 </span> 239 </li> 240 } 241 } 242 243 @if (layout == "dropdown" && type == "currency") { 244 foreach (var currency in currencies) 245 { 246 var link = $"{url.Scheme}://{hostName}?ID={currentPage.ID}&AreaID={Pageview.Area.ID}&CurrencyCode={currency.Code}"; 247 link += groupId != "" ? $"&GroupID={groupId}" : ""; 248 link += productId != "" ? $"&ProductID={productId}" : ""; 249 link += variantId != "" ? $"&VariantID={variantId}" : ""; 250 251 <li> 252 <a href="@link" class="dropdown-item nav-link-underline @linkFontSize @linkFontWeight @linkCasing">@currency.Code</a> 253 </li> 254 } 255 } 256 257 258 259 @helper RenderIcon(string icon, string iconSize) 260 { 261 if (Path.GetExtension(icon).ToLower() == ".svg" && !icon.ToLower().Contains("none")) 262 { 263 string iconPath = Dynamicweb.Context.Current.Server.MapPath(icon); 264 265 <span class="@iconSize"> 266 @ReadFile(iconPath) 267 </span> 268 } 269 } 270 271