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_05a25f10afea4b12ace1888aad190879.Execute() in D:\dynamicweb.net\Solutions\CO3\granzow2023.cloud.dynamicweb-cms.com\Files\Templates\Designs\Swift\_parsed\Swift_Preferences.parsed.cshtml:line 209
   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.Content.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.GetName(Pageview.Area.EcomLanguageId)) : 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 @* Modal *@ 64 65 @if (layout == "modal") 66 { 67 int updatePageId = pageId; 68 69 <div class="modal-content @modalTheme item_@Model.Item.SystemName.ToLower()"> 70 <div class="modal-header"> 71 <h5 class="modal-title" id="PreferencesModalLabel">@Translate("Preferences")</h5> 72 <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> 73 </div> 74 <div class="modal-body d-grid gap-3" id="PreferencesModalBody"> 75 <input type="hidden" name="CurrentPageID" value="@pageId"> 76 @if (groupId != "") { 77 <input type="hidden" name="GroupID" value="@groupId"> 78 } 79 80 @if (productId != "") { 81 <input type="hidden" name="ProductID" value="@productId"> 82 } 83 84 @if (variantId != "") { 85 <input type="hidden" name="VariantID" value="@variantId"> 86 } 87 88 89 @if (languages.Count() > 1 && Model.Item.GetBoolean("LanguageSelector")) 90 { 91 <form action="/Default.aspx" data-response-target-element="PreferencesModalContent" data-layout-template="Swift_Preferences.cshtml" data-preloader="inline"> 92 <input type="hidden" name="Layout" value="modal"> 93 <input type="hidden" name="CurrentPageID" value="@pageId"> 94 <input type="hidden" name="GroupID" value="@groupId"> 95 <input type="hidden" name="ProductID" value="@productId"> 96 <input type="hidden" name="VariantID" value="@variantId"> 97 98 <div class="form-floating"> 99 <select id="LanguageSelect" name="ID" class="form-select d-col-12" aria-label="@Translate("Select language")" onchange="swift.PageUpdater.Update(event)"> 100 @foreach (var language in languages) 101 { 102 if (language != null && language.Published && language.Area.Active && language.Area.Published) { 103 string selectedLanguage = language.Area.ID == Pageview.Area.ID ? "selected" : ""; 104 updatePageId = language.Area.ID == Pageview.Area.ID ? language.ID : updatePageId; 105 106 var masterPageId = Pageview.Page?.MasterPageId != null && Pageview.Page.MasterPageId != 0 ? Pageview.Page.MasterPageId : Pageview.ID; 107 var preferencePageId = Dynamicweb.Content.Services.Pages.GetPageIDByMasterID(masterPageId, language.AreaId); 108 preferencePageId = preferencePageId == 0 ? Pageview.Page.MasterPageId : preferencePageId; 109 preferencePageId = preferencePageId == 0 ? Pageview.Page.ID : preferencePageId; 110 111 <option hreflang="@language.Area.CultureInfo.TwoLetterISOLanguageName" value="@preferencePageId" @selectedLanguage>@language.Area.DisplayName</option> 112 } 113 } 114 </select> 115 <label for="LanguageSelect">@Translate("Language")</label> 116 </div> 117 </form> 118 } 119 120 @if (ecomCountries.Count() > 1 && Model.Item.GetBoolean("CountrySelector")) 121 { 122 <form action="/Default.aspx" data-response-target-element="PreferencesModalContent" data-layout-template="Swift_Preferences.cshtml" data-preloader="inline"> 123 <input type="hidden" name="ID" value="@Pageview.ID"> 124 <input type="hidden" name="CurrentPageID" value="@pageId"> 125 <input type="hidden" name="GroupID" value="@groupId"> 126 <input type="hidden" name="ProductID" value="@productId"> 127 <input type="hidden" name="VariantID" value="@variantId"> 128 129 <div class="form-floating"> 130 <select name="CountryCode" id="CountryCodeSelect" class="form-select d-col-12" aria-label="@Translate("Select country")" onchange="swift.PageUpdater.Update(event)"> 131 @foreach (var country in ecomCountries) 132 { 133 var selectedCountry = country.Code2 == Dynamicweb.Ecommerce.Common.Context.Country.Code2 ? "selected" : ""; 134 135 if (!string.IsNullOrEmpty(country.GetName(Pageview.Area.EcomLanguageId))) { 136 <option value="@country.Code2" @selectedCountry>@country.GetName(Pageview.Area.EcomLanguageId)</option> 137 } 138 } 139 </select> 140 <label for="CountryCodeSelect">@Translate("Delivery country")</label> 141 </div> 142 </form> 143 } 144 145 @if (currencies.Count() > 1 && Model.Item.GetBoolean("CurrencySelector")) 146 { 147 <form action="/Default.aspx" data-response-target-element="PreferencesModalContent" data-layout-template="Swift_Preferences.cshtml" data-preloader="inline"> 148 <input type="hidden" name="ID" value="@Pageview.ID"> 149 <input type="hidden" name="CurrentPageID" value="@pageId"> 150 <input type="hidden" name="GroupID" value="@groupId"> 151 <input type="hidden" name="ProductID" value="@productId"> 152 <input type="hidden" name="VariantID" value="@variantId"> 153 154 <div class="form-floating"> 155 <select name="CurrencyCode" id="CurrencySelect" class="form-select d-col-12" aria-label="@Translate("Select currency")" onchange="swift.PageUpdater.Update(event)"> 156 @foreach (var currency in currencies) 157 { 158 string selectedCurrency = currency.Code == Dynamicweb.Ecommerce.Common.Context.Currency.Code ? "selected" : ""; 159 160 if (!string.IsNullOrEmpty(currency.Code)) { 161 <option value="@currency.Code" @selectedCurrency>@currency.Code</option> 162 } 163 } 164 </select> 165 <label for="CurrencySelect">@Translate("Currency")</label> 166 </div> 167 </form> 168 } 169 </div> 170 <div class="modal-footer"> 171 @{ 172 hostName = !string.IsNullOrEmpty(Pageview.Area.DomainLock) ? Pageview.Area.DomainLock : url.Host; 173 string link = $"Default.aspx?ID={updatePageId}"; 174 link += groupId != "" ? $"&GroupID={groupId}" : ""; 175 link += productId != "" ? $"&ProductID={productId}" : ""; 176 link += variantId != "" ? $"&VariantID={variantId}" : ""; 177 var friendlyUrl = $"{url.Scheme}://{hostName}{Dynamicweb.Frontend.SearchEngineFriendlyURLs.GetFriendlyUrl(link)}"; 178 } 179 180 <a href="@friendlyUrl" class="btn btn-primary w-100">@Translate("Update preferences")</a> 181 </div> 182 </div> 183 } 184 185 @* Dropdowns *@ 186 187 @if (layout == "dropdown" && type == "country") { 188 foreach (var country in ecomCountries) 189 { 190 var link = $"{url.Scheme}://{hostName}?ID={currentPage.ID}&AreaID={Pageview.Area.ID}&CountryCode={country.Code2}"; 191 link += groupId != "" ? $"&GroupID={groupId}" : ""; 192 link += productId != "" ? $"&ProductID={productId}" : ""; 193 link += variantId != "" ? $"&VariantID={variantId}" : ""; 194 195 if (!string.IsNullOrEmpty(country.GetName(Pageview.Area.EcomLanguageId))) { 196 <li> 197 <a href="@link" class="dropdown-item nav-link-underline @linkFontSize @linkFontWeight @linkCasing">@country.GetName(Pageview.Area.EcomLanguageId)</a> 198 </li> 199 } 200 } 201 } 202 203 @if (layout == "dropdown" && type == "language") { 204 string iconFolder = "/Files/FlagIcons/"; 205 int languagesCount = 0; 206 207 foreach (var language in languages) 208 { 209 if (language.Area != null) { 210 if (language != null && language.Published && language.Area.Active && language.Area.Published && language.Area.ID != Dynamicweb.Frontend.PageView.Current().AreaID) { 211 hostName = !string.IsNullOrEmpty(language.Area.DomainLock) ? language.Area.DomainLock : url.Host; 212 string link = $"Default.aspx?ID={language.ID}"; 213 link += groupId != "" ? $"&GroupID={groupId}" : ""; 214 link += productId != "" ? $"&ProductID={productId}" : ""; 215 link += variantId != "" ? $"&VariantID={variantId}" : ""; 216 217 var friendlyUrl = $"{url.Scheme}://{hostName}{Dynamicweb.Frontend.SearchEngineFriendlyURLs.GetFriendlyUrl(link)}"; 218 219 string icon = iconFolder + language.Area.CultureInfo.Name.ToLower() + ".svg"; 220 <li> 221 <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">@language.Area.DisplayName</span></a> 222 </li> 223 224 languagesCount++; 225 } 226 } 227 } 228 229 if (languagesCount == 0) { 230 <li> 231 <span class="dropdown-item fs-7 fw-normal"> 232 @Translate("Page is not translated") 233 </span> 234 </li> 235 } 236 } 237 238 @if (layout == "dropdown" && type == "currency") { 239 foreach (var currency in currencies) 240 { 241 var link = $"{url.Scheme}://{hostName}?ID={currentPage.ID}&AreaID={Pageview.Area.ID}&CurrencyCode={currency.Code}"; 242 link += groupId != "" ? $"&GroupID={groupId}" : ""; 243 link += productId != "" ? $"&ProductID={productId}" : ""; 244 link += variantId != "" ? $"&VariantID={variantId}" : ""; 245 246 <li> 247 <a href="@link" class="dropdown-item nav-link-underline @linkFontSize @linkFontWeight @linkCasing">@currency.Code</a> 248 </li> 249 } 250 } 251 252 @helper RenderIcon(string icon, string iconSize) 253 { 254 if (Path.GetExtension(icon).ToLower() == ".svg" && !icon.ToLower().Contains("none")) 255 { 256 string iconPath = Dynamicweb.Context.Current.Server.MapPath(icon); 257 258 <span class="@iconSize"> 259 @ReadFile(iconPath) 260 </span> 261 } 262 } 263 264