تهیه خروجی RSS در برنامههای ASP.NET MVC
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۰۶/۲۰ ۱۷:۵۱
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
FeedResult(string feedTitle, IList<FeedItem> rssItems, string language = "fa-IR")
using System;
namespace MvcRssHelper
{
public class FeedItem
{
public string Title { set; get; }
public string AuthorName { set; get; }
public string Content { set; get; }
public string Url { set; get; }
public DateTime LastUpdatedTime { set; get; }
}
}
using System;
namespace MvcRssApplication.Models
{
public class Post
{
public int Id { set; get; }
public string Title { set; get; }
public string AuthorName { set; get; }
public string Body { set; get; }
public DateTime Date { set; get; }
}
}
using System;
using System.Collections.Generic;
using MvcRssApplication.Models;
namespace MvcRssApplication.DataSource
{
public static class BlogItems
{
public static IList<Post> GetLastBlogPostsList()
{
var results = new List<Post>();
for (int i = 1; i < 21; i++)
{
results.Add(new Post
{
AuthorName = "شخص " + i,
Body = "مطلب " + i,
Date = DateTime.Now.AddDays(-i),
Id = i,
Title = "عنوان "+i
});
}
return results;
}
}
}
using System.Collections.Generic;
using System.Web.Mvc;
using MvcRssApplication.DataSource;
using MvcRssApplication.Models;
using MvcRssHelper;
namespace MvcRssApplication.Controllers
{
public class HomeController : Controller
{
const int Min15 = 900;
[OutputCache(Duration = Min15)]
public ActionResult Index()
{
var list = BlogItems.GetLastBlogPostsList();
var feedItemsList = mapPostsToFeedItems(list);
return new FeedResult("فید مطالب سایت ما", feedItemsList);
}
private List<FeedItem> mapPostsToFeedItems(IList<Post> list)
{
var feedItemsList = new List<FeedItem>();
foreach (var item in list)
{
feedItemsList.Add(new FeedItem
{
AuthorName = item.AuthorName,
Content = item.Body,
LastUpdatedTime = item.Date,
Title = item.Title,
//این آدرس باید مطلق باشد به نحو زیر
Url = this.Url.Action(actionName: "Details", controllerName: "Post", routeValues: new { id = item.Id }, protocol: "http")
});
}
return feedItemsList;
}
}
}
<a10:updated>2012-10-20T16:09:13+03:30</a10:updated>
<pubDate>Sat, 30 Jan 2010 02:26:32 -0800</pubDate>
private static readonly Regex _matchHexadecimalSymbols =
new Regex("[\x00-\x08\x0B\x0C\x0E-\x1F]", RegexOptions.IgnoreCase | RegexOptions.Compiled);
/// <summary>
/// there are a lot of symbols which can't be in xml code.
/// </summary>
public static string RemoveHexadecimalSymbols(this string txt)
{
return string.IsNullOrWhiteSpace(txt) ?
string.Empty : _matchHexadecimalSymbols.Replace(txt, string.Empty);
} <link title="فید آخرین تغییرات سایت" href="/rss.xml" type="application/rss+xml" rel="alternate" />
PM> Install-Package DNTCommon.Web.Core
FeedImageContentPath = "",
FeedImageTitle = "",