代码改变世界

ASP.NET MVC 扩展之 RssResult

2012-12-18 18:59  音乐让我说  阅读(305)  评论(0编辑  收藏  举报

直接贴代码了:

[Serializable]
public class RssItem
{
    public string Description { get; set; }

    public string Link { get; set; }

    public DateTime PublishedDate { get; set; }

    public string RssImage { get; set; }

    public string Title { get; set; }
}

 

扩展:

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Xml;

namespace MvcUI.Extensions
{
    public class RssResult : ActionResult
    {
        private readonly string _description;
        private readonly List<RssItem> _items;
        private readonly string _title;

        public RssResult(IEnumerable<RssItem> items, string title, string description)
        {
            this._items = new List<RssItem>(items);
            this._title = title;
            this._description = description;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            XmlWriterSettings settings2 = new XmlWriterSettings
            {
                Indent = true,
                NewLineHandling = NewLineHandling.Entitize
            };
            XmlWriterSettings settings = settings2;
            context.HttpContext.Response.ContentType = "text/xml";
            Action<RssItem> action = null;
            using (XmlWriter _writer = XmlWriter.Create(context.HttpContext.Response.OutputStream, settings))
            {
                _writer.WriteStartElement("rss");
                _writer.WriteAttributeString("version", "2.0");
                _writer.WriteStartElement("channel");
                _writer.WriteElementString("title", this._title);
                _writer.WriteElementString("description", this._description);
                _writer.WriteElementString("link", context.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority));
                if (action == null) action = delegate(RssItem x)
                    {
                        _writer.WriteStartElement("item");
                        _writer.WriteElementString("title", x.Title);
                        _writer.WriteElementString("description", x.Description);
                        _writer.WriteElementString("pubDate", x.PublishedDate.ToString("o"));
                        _writer.WriteElementString("link", context.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority) + x.Link);
                        if (!string.IsNullOrEmpty(x.RssImage))
                        {
                            _writer.WriteStartElement("image");
                            _writer.WriteElementString("url", context.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority) + x.RssImage);
                            _writer.WriteElementString("title", x.Title);
                            _writer.WriteElementString("link", context.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority) + x.Link);
                            _writer.WriteEndElement();
                        }
                        _writer.WriteEndElement();
                    };
                this._items.ForEach(action);
                _writer.WriteEndElement();
                _writer.WriteEndElement();
            }
        }
    }
}

 

 

谢谢浏览!