Asp.net mvc 如何处理ASP.NET MVC局部视图的异常?

Asp.net mvc 如何处理ASP.NET MVC局部视图的异常?,asp.net-mvc,asp.net-mvc-2,exception-handling,Asp.net Mvc,Asp.net Mvc 2,Exception Handling,我有一个局部视图(ascx),用于显示个人RSS提要的前x篇文章: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Models.Article>>" %> <% foreach (var item in Model) { %> <a href="<%: item.Url %>"><

我有一个局部视图(ascx),用于显示个人RSS提要的前x篇文章:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Models.Article>>" %>

    <% foreach (var item in Model) { %>

        <a href="<%: item.Url %>"><%: item.Title %></a><br />
        <%: String.Format("{0:g}", item.Date) %><br />
        <%: item.Body %>
        <br />

    <% } %>

我个人不会将文章的检索作为视图模板的一部分。我不认为这是数据(控制器和模型)的收集和处理与数据(视图)的表示之间的清晰分离


您可能希望尝试在控制器甚至模型中检索该文章列表,这取决于您处理服务风格依赖关系的方式(在这方面,我更倾向于控制器)。然后让您的视图能够处理将文章列表传递给partial(如果它有一个或显示在数据收集过程中发生的任何异常)。

更干净的方法是使用调用操作的
RenderAction
,该操作加载提要数据,然后可以捕获错误

RssWidget action在ServicesController上(实际上可以驻留在每个控制器中,这无关紧要,但我认为这是一个干净的解决方案):
此操作从提要加载文章。请注意,代码只是演示,您必须通过加载文章的实现来替换
RssService.GetArticles()
。如果一切顺利,将返回一个PartialView(名为RssWidget.ascx),其中显示文章列表

public ServicesController : Controller
{
    public ActionResult RssWidget()
    {
        try
        { 
            var articles = RssService.GetArticles() // or whatever the articles'
                                                    // source is
            return PartialView(articles);
        }
        catch
        {
            return PartialView("Error");
        }
    }
}
RssWidget.ascx:这是用于呈现文章列表的(部分)视图。别忘了用文章的实际类型替换
文章

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Article>>" %>

<% foreach(var article in Model) { %>
    <div class="article">
        <h3><%= Html.Encode(article.Title) %></h3>
        ...
    </div>
<% } %>

我喜欢这个想法,但是模型是如何传递给控制器的呢?对不起,我把一些东西弄混了。我现在扩展了我的答案,我希望它现在更清楚。
<div id="articleList" class="section">
    <div class="sectionTitle">My Recent Articles</div>
    <hr />
    <div class="sectionBody">
        <% Html.RenderAction("ArticleList", "ArticleList", new { feedUrl = Model.RSSFeed }); %>
    </div>
</div>
public class ArticleListController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [ChildActionOnly]
    public ActionResult ArticleList(string feedUrl)
    {
        try
        {
            IEnumerable<Models.Article> articles = Helpers.ArticleFeedHelper.GetArticles(feedUrl);
            return PartialView(articles);
        }
        catch (Exception ex)
        {
            // Handle the error and return an error partial view
        }
    }
}
routes.MapRoute(
    "ArticleList", // Route name
    "{controller}/{action}/{feedUrl}", // URL with parameters
    new { controller = "ArticleList", action = "ArticleList", feedUrl = "" } // Parameter defaults
);
public ServicesController : Controller
{
    public ActionResult RssWidget()
    {
        try
        { 
            var articles = RssService.GetArticles() // or whatever the articles'
                                                    // source is
            return PartialView(articles);
        }
        catch
        {
            return PartialView("Error");
        }
    }
}
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<Article>>" %>

<% foreach(var article in Model) { %>
    <div class="article">
        <h3><%= Html.Encode(article.Title) %></h3>
        ...
    </div>
<% } %>
...
<div id="articleList" class="section">
    <div class="sectionTitle">My Recent Articles</div>
    <hr />
    <div class="sectionBody">
        <% Html.RenderAction("RssWidget", "Services"); %>
    </div>
</div>
...