Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to return Rss from ApiController in MVC 4
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/12437731/create-rss-feed-in-mvc4-webapi">Create RSS feed in MVC4/WebAPI</a> </p> </blockquote> <p>I want my controller to return rss. In MVC 3, I just create a <code>Controller</code> derived from <code>Controller</code>, <code>RssResult</code> to write rss, derived from <code>ActionResult</code>, then I return it in the controller, and everything works well.</p> <p>My problem is, how can I do that with <code>Controller</code> derived from <code>ApiController</code>?</p> <p>Thanks for your help :)</p> <p><strong>----------------Update----------------</strong></p> <p>Here is my controller:</p> <pre><code>public class HomeController : Controller { public ActionResult Index() { List&lt;IRss&gt; list = new List&lt;IRss&gt; { new Product { Title = "Laptop", Description = "&lt;strong&gt;The latest model&lt;/strong&gt;", 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"); } </code></pre> <p>Here is my <code>RssResult</code></p> <pre><code>public class RssResult : ActionResult { private List&lt;IRss&gt; items; private string title; private string description; /// &lt;summary&gt; /// Initialises the RssResult /// &lt;/summary&gt; /// &lt;param name="items"&gt;The items to be added to the rss feed.&lt;/param&gt; /// &lt;param name="title"&gt;The title of the rss feed.&lt;/param&gt; /// &lt;param name="description"&gt;A short description about the rss feed.&lt;/param&gt; public RssResult(List&lt;IRss&gt; 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 =&gt; { 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(); } } } </code></pre> <p>Everything works well. But now, my controller changes into <code>public class HomeController : ApiController</code>. Now, what should I do to make this new controller work as the old one? How should I define the new <code>Index</code> method?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload