My asp.net site allows users to pick the theme they want from a list generated from the app_themes folder. From time to time, themes are renamed or removed. Any user who has selected a deleted theme name (it is stored in a cookie) will get the exception:
Theme 'XXX' cannot be found in the application or global theme directories
Stack Trace:
[HttpException (0x80004005): Theme 'test' cannot be found in the application or global theme directories.]
System.Web.Compilation.ThemeDirectoryCompiler.GetThemeBuildResultType(String themeName) +920
System.Web.Compilation.ThemeDirectoryCompiler.GetThemeBuildResultType(HttpContext context, String themeName) +73
System.Web.UI.Page.InitializeThemes() +8699455
System.Web.UI.Page.PerformPreInit() +38
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +282
Where is the best place to trap and handle this exception?
if you use cookies to store user selected theme and you get error 'xxx'theme not found in local or global directory, then make sure that your cookie name is not same with another cookie name.
In the Page_PreInit method where you assign themes, there's a couple of ways to deal with it. What I do is check to make sure that the directory exists. If it does, then that's the theme I want. If it doesn't, then use a default theme where I know the directory exists.
void Page_PreInit(object sender, EventArgs e)
{
if (ViewState["PageTheme"] == null)
{
if (!Directory.Exists("~/App_Themes/THEMENAME_TO_LOOK_FOR"))
{
Theme = "DEFAULT_THEME"
}
else
{
Theme = "THEMENAME_TO_LOOK_FOR";
}
ViewState["PageTheme"] = Theme;
}
else
{
Theme = ViewState["PageTheme"].ToString();
}
}
I usually store in the viewstate so I don't have to recheck every time but if you're changing themes on-the-fly, then you'll probably need to not do that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With