1

可能重复:
在 MVC4/WebAPI 中创建 RSS 提要

我希望我的控制器返回 rss。在 MVC 3 中,我只是创建了一个Controller派生自ControllerRssResult写 rss,派生自ActionResult,然后我在控制器中返回它,一切正常。

我的问题是,我怎么能用Controller派生来做到这一点ApiController

谢谢你的帮助 :)

- - - - - - - - 更新 - - - - - - - -

这是我的控制器:

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");
    }

这是我的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();
        }
    }
}

一切正常。但是现在,我的控制器变成了public class HomeController : ApiController. 现在,我应该怎么做才能让这个新控制器像旧控制器一样工作?我应该如何定义新Index方法?

4

1 回答 1

0

AFAIK,甚至 ASP.NET MVC 也没有任何内置的 RssResult。您还必须实现类似于以下 WebAPI 的 SO 答案中提到的内容。

ASP.NET MVC 中的 RSS 源

于 2013-01-18T05:26:13.263 回答