1

Possible Duplicate:
Create RSS feed in MVC4/WebAPI

I want my controller to return rss. In MVC 3, I just create a Controller derived from Controller, RssResult to write rss, derived from ActionResult, then I return it in the controller, and everything works well.

My problem is, how can I do that with Controller derived from ApiController?

Thanks for your help :)

----------------Update----------------

Here is my controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        List<IRss> list = new List<IRss>
                              {
                                  new Product
                                      {
                                          Title = "Laptop",
                                          Description = "<strong>The latest model</strong>",
                                          Link = "/dell"
                                      },
                                  new Product
                                      {
                                          Title = "Car",
                                          Description = "I'm the fastest car",
                                          Link = "/car"
                                      },
                                  new Product
                                      {
                                          Title = "Cell Phone",
                                          Description = "The latest technology",
                                          Link = "/cellphone"
                                      }
                              };
        return new RssResult(list, "The best products", "Here is our best product");
    }

Here is my RssResult

public class RssResult : ActionResult
{
    private List<IRss> items;
    private string title;
    private string description;

    /// <summary>
    /// Initialises the RssResult
    /// </summary>
    /// <param name="items">The items to be added to the rss feed.</param>
    /// <param name="title">The title of the rss feed.</param>
    /// <param name="description">A short description about the rss feed.</param>
    public RssResult(List<IRss> items, string title, string description)
    {
        this.items = items;
        this.title = title;
        this.description = description;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        XmlWriterSettings settings = new XmlWriterSettings
                                         {Indent = true, NewLineHandling = NewLineHandling.Entitize};

        context.HttpContext.Response.ContentType = "text/xml";

        using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.OutputStream, settings))
        {
            // Begin structure
            writer.WriteStartElement("rss");
            writer.WriteAttributeString("version", "2.0");
            writer.WriteStartElement("channel");

            writer.WriteElementString("title", title);
            writer.WriteElementString("description", description);
            writer.WriteElementString("link", context.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority));

            // Individual item
            items.ForEach(x =>
                              {
                                  writer.WriteStartElement("item");
                                  writer.WriteElementString("title", x.Title);

                                  writer.WriteStartElement("description");
                                  writer.WriteCData(x.Description);
                                  writer.WriteEndElement();
                                  writer.WriteElementString("pubDate", DateTime.Now.ToShortDateString());

                                  writer.WriteElementString("link",
                                                            context.HttpContext.Request.Url.GetLeftPart(
                                                                UriPartial.Authority) + x.Link);
                                  writer.WriteEndElement();
                              });

            // End structure
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
}

Everything works well. But now, my controller changes into public class HomeController : ApiController. Now, what should I do to make this new controller work as the old one? How should I define the new Index method?

Community
  • 1
  • 1
Triet Doan
  • 11,455
  • 8
  • 36
  • 69
  • 1
    Create a mediatypeformatter as done in this answer [Create RSS feed in MVC4/WebAPI](http://stackoverflow.com/questions/12437731/create-rss-feed-in-mvc4-webapi) – heads5150 Jan 18 '13 at 05:31
  • You mean I have to implement SyndicationFeedFormatter myself? But I don't use it. I only use XmlWriter to write the rss to response stream. – Triet Doan Jan 18 '13 at 08:48
  • Yes I mean you have to implement SyndicationFeedformatter yourself if you want to use WebAPI as it's attended. – heads5150 Jan 18 '13 at 08:53
  • I've updated my question with my source code. If I want to use XmlWriter, what should I do? It seems SyndicationFeedFormatter is too complicated for me. – Triet Doan Jan 18 '13 at 08:55

1 Answers1

0

AFAIK, even ASP.NET MVC doesn't have any built-in RssResult. You would have to implement something similar to what's mentioned in following SO answer for WebAPI as well.

RSS Feeds in ASP.NET MVC

Community
  • 1
  • 1
Bhavesh Chauhan
  • 1,048
  • 8
  • 5
  • I've read the post on your link before. It works well with normal controller, derived from `Controller`, but it doesn't work with controller derived from `ApiController`. – Triet Doan Jan 18 '13 at 08:45