我有一个局部视图(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 /> <% } %>另外,我有一个方法,它采用RSS URL并返回类型文章的IEnumerable:
public static class ArticleFeedHelper { public static IEnumerable<Models.Article> GetArticles(string feedUrl) { // Uncaught exception can happen here (e.g. 404, malformed RSS, etc.) } }在我的一个观点中,我将局部视图称为:
<div id="articleList" class="section"> <div class="sectionTitle">My Recent Articles</div> <hr /> <div class="sectionBody"> <% Html.RenderPartial("ArticleList", ArticleFeedHelper.GetArticles(Model.RSSFeed)); %> </div> </div>问题是我想向主视图显示错误消息。 仅仅因为无法检索RSS提要不应该如此具有破坏性,以至于它会将用户抛向完整的错误页面。
解析度:
局部视图和ArticleFeedHelper保持不变。
使用Dave的建议,主视图更改为:
<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 );I have a partial view (ascx) for displaying the top x articles of a person's RSS feed:
<%@ 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 /> <% } %>In addition, I have a method that takes an RSS URL and returns an IEnumerable of type Article:
public static class ArticleFeedHelper { public static IEnumerable<Models.Article> GetArticles(string feedUrl) { // Uncaught exception can happen here (e.g. 404, malformed RSS, etc.) } }On one of my views, I call the partial view like this:
<div id="articleList" class="section"> <div class="sectionTitle">My Recent Articles</div> <hr /> <div class="sectionBody"> <% Html.RenderPartial("ArticleList", ArticleFeedHelper.GetArticles(Model.RSSFeed)); %> </div> </div>The problem is that I would like to surface an error message to the main view. Just because the RSS feed cannot be retrieved should not be so disruptive that it jettisons the user to a full error page.
RESOLUTION:
The partial view, and ArticleFeedHelper stayed the same.
Using Dave's suggestions, the main view was changed to:
<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>A partial controller was added:
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 } } }And a route was added:
routes.MapRoute( "ArticleList", // Route name "{controller}/{action}/{feedUrl}", // URL with parameters new { controller = "ArticleList", action = "ArticleList", feedUrl = "" } // Parameter defaults );最满意答案
更RenderAction方法是使用调用动作的RenderAction ,它会加载Feed数据然后捕获错误。
ServicesController上的RssWidget操作 (实际上可以驻留在每个控制器中,无所谓,但我认为,这是一个干净的解决方案): 此操作会从Feed中加载文章。 请注意,代码只是演示,您必须通过加载文章的实现替换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 :这是用于呈现文章列表的(部分)视图。 不要忘记用Article的实际类型替换文章:
<%@ 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> <% } %>显示RssWidget 的视图 。 您可以在任何视图中渲染此“小部件”。 RenderAction("RssWidget", "Services")告诉框架在ServicesController上执行RssWidget操作并将结果插入实际视图中。
... <div id="articleList" class="section"> <div class="sectionTitle">My Recent Articles</div> <hr /> <div class="sectionBody"> <% Html.RenderAction("RssWidget", "Services"); %> </div> </div> ...The cleaner way would be to use RenderAction that calls an action, which loads the feed data and can then catch errors.
RssWidget action on ServicesController (could actually reside in every controller, doesn't matter, but I think, that's a clean solution): This action loads the articles from the feed. Note that the code is only demo and you have to replace RssService.GetArticles() by your implementation of loading the articles. If all goes right, a PartialView (named RssWidget.ascx) is returned, that shows a list of articles.
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: This is the (partial) view which is used to render the list of articles. Don't forget to replace Article with your actual Type for the articles:
<%@ 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> <% } %>The View where the RssWidget is displayed. You can render this "widget" in any view, you want to. RenderAction("RssWidget", "Services") tells the framework to execute the RssWidget action on the ServicesController and to insert the result in the actual view.
... <div id="articleList" class="section"> <div class="sectionTitle">My Recent Articles</div> <hr /> <div class="sectionBody"> <% Html.RenderAction("RssWidget", "Services"); %> </div> </div> ...更多推荐
RSS,<%,view,ArticleFeedHelper,class,电脑培训,计算机培训,IT培训"/> <meta n
发布评论