Asp.mvc - 覆盖partialtemplates未找到的行为

时间:2010-12-16 18:08:27

标签: asp.net-mvc templates

无法找到部分模板时mvc的默认行为是抛出错误。我正在编写一个自定义视图引擎,它将从默认位置以外的不同位置检索部分内容。我还想改变行为,如果找不到部分模板,那么就不会抛出错误,而是输入模板应该输出的“空白”然后让我的记录器记录事实上,找不到模板。这样做的原因是我们需要多个人能够提交“帮助”模板以提供一些额外的文本,但如果找不到,则不会抛出错误,因为它们对屏幕的功能并不重要。

1 个答案:

答案 0 :(得分:0)

没关系,我刚刚创建了一个不同的部分模板,如果找不到模板,我会想要返回空白区域。

 public static class PartialExtensions
    {
        // Methods
        public static MvcHtmlString PartialWithErrorRedirection(this HtmlHelper htmlHelper, string partialViewName, string redirectViewName)
        {
            try
            {
                return htmlHelper.Partial(partialViewName, null, htmlHelper.ViewData);
            }
            catch (InvalidOperationException ex)
            {
                if (redirectViewName != null && redirectViewName.Trim().Length > 0)
                    return (htmlHelper.Partial(redirectViewName));
                else
                    return (htmlHelper.Partial("NotFound"));
            }
        }





    }