开发者

Asp.mvc - override not found behaviour for partialtemplates

The default behaviour of mvc when a partial template can not be foun开发者_如何学God is to throw an error. I am writing a custom view engine that will retrieve partials from different locations than the default. I also want to change the behaviour such that if a partial template can not be found, then an error won't be thrown but rather a "blank" will be entered where the template was supposed to be output and then have my logger log the fact the template could not be found. The reason for this is that we need multiple people to be able to submit "help" templates that provide some additional text, but if not found, will not throw an error because they are not important to the functioning of the screen.


Never mind, I just created a different partial template that's called in the areas I want to return empty space if the template is not found.

 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"));
            }
        }





    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜