namespace System.Web.Mvc
{
public abstract class ActionResult
public abstract void ExecuteResult(ControllerContext context);
}
using System;
using System.ServiceModel.Syndication;
using System.Text;
using System.Web.Mvc;
using System.Xml;
namespace RssMvcApplication.Web.Mvc
///<summary>
/// Encapsulates the Syndication result of an action method and is used to perform a
/// framework-level operation on the action method's behalf.
///</summary>
public class SyndicationResult : ActionResult
private readonly SyndicationFeedFormatter _formatter;
private const string RssContentType = "application/rss+xml";
private const string AtomContentType = "application/atom+xml";
private readonly string _contentType;
/// Initializes a new instance of the <see cref="SyndicationResult"/> class.
///<param name="formatter"></param>
public SyndicationResult(SyndicationFeedFormatter formatter)
if (formatter.Feed == null)
throw new ArgumentException("formatter parameter is not initialized with a feed.", "formatter");
if (formatter is Atom10FeedFormatter)
_contentType = AtomContentType;
if (formatter is Rss20FeedFormatter)
_contentType = RssContentType;
_formatter = formatter;
/// Enables processing of the result of an action method by a custom
/// type that inherits from <see cref="T:System.Web.Mvc.ActionResult"/>.
///<param name="context">The context within which the result is executed.</param>
public override void ExecuteResult(ControllerContext context)
context.HttpContext.Response.ContentType = _contentType;
var ws = new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8, OmitXmlDeclaration = true };
using (var writer = XmlWriter.Create(context.HttpContext.Response.Output, ws))
if (writer != null) _formatter.WriteTo(writer);
< ul id="menu">
<li><%= Html.ActionLink("Home", "Index", "Home")%></li>
<li><%= Html.ActionLink("About", "About", "Home")%></li>
<li><%= Html.ActionLink("Rss", "Rss", "Home")%></li>
<li><%= Html.ActionLink("Atom", "Atom", "Home")%></li>
</ ul >
using System.Linq;
using RssMvcApplication.Domain;
using RssMvcApplication.Services;
using RssMvcApplication.Web.Mvc;
namespace RssMvcApplication.Controllers
[HandleError]
public class HomeController : Controller
private readonly SearchService _searchService;
public HomeController()
_searchService = new SearchService();
public ActionResult Rss()
var syndicationFeed = GetSyndicationFeed();
return new SyndicationResult(new Rss20FeedFormatter(syndicationFeed));
public ActionResult Atom()
return new SyndicationResult(new Atom10FeedFormatter(syndicationFeed));
private SyndicationFeed GetSyndicationFeed()
var results = _searchService.GetResults();
return new SyndicationFeed
Title = new TextSyndicationContent(@"Tech Head Brothers"),
Description = new TextSyndicationContent(@"Feed description"),
Items = results.ToList().ConvertAll(result => result.ToSyndicationItem())
};
public ActionResult Index()
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
public ActionResult About()
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace RssMvcApplication.Services
public class SearchService
public IEnumerable<Result> GetResults()
return new Collection<Result>
new Result {Title = "1ere Titre", Description = "Ceci est la description du 1er titre."},
new Result {Title = "2eme Titre", Description = "Ceci est la description du 2eme titre."}
namespace RssMvcApplication.Domain
public class Result
public string Title { get; set; }
public string Description { get; set; }
public static class Helpers
/// Convert Domain Result to a SyndicationItem.
///<param name="result">The result.</param>
///<returns></returns>
public static SyndicationItem ToSyndicationItem(this Result result)
return new SyndicationItem(result.Title, result.Description, new Uri("http://www.techheadbrothers.com"));