我的asp.net站点允许用户从app_themes文件夹生成的列表中选择他们想要的主题.有时会重命名或删除主题.选择已删除主题名称的任何用户(它存储在cookie中)都将获得异常:
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
Run Code Online (Sandbox Code Playgroud)
陷阱和处理此异常的最佳位置在哪里?
在分配主题的 Page_PreInit 方法中,有几种方法可以处理它。我所做的就是检查以确保该目录存在。如果是的话,那就是我想要的主题。如果没有,则使用我知道该目录存在的默认主题。
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我通常存储在视图状态中,因此不必每次都重新检查,但如果您要动态更改主题,那么您可能不需要这样做。
| 归档时间: |
|
| 查看次数: |
16497 次 |
| 最近记录: |