A rss is good for SEO because it tells search engines what pages of your site it should index. But how do we create a rss in ASP.NET Core? In this post I will show you a very simple way to generate a rss that works in ASP.NET Core web projects.
Sample XML rss
<rss version="2.0">
<channel xmlns:atom="http://www.w3.org/2005/Atom">
<title>nopCommerce demo store: Blog</title>
<description>Blog</description>
<link>https://demo.nopcommerce.com/</link>
<lastBuildDate>Sun, 29 Mar 2020 04:00:37 GMT</lastBuildDate>
<atom:link href="https://demo.nopcommerce.com/blog/rss/1" rel="self" type="application/rss+xml" />
<item>
<guid isPermaLink="false">urn:store:1:blog:post:2</guid>
<link>https://demo.nopcommerce.com/why-your-online-store-needs-a-wish-list</link>
<title>Why your online store needs a wish list</title>
<description><p>What canswer to ure.</p></description>
</item>
<item>
<guid isPermaLink="false">urn:store:1:blog:post:1</guid>
<link>https://demo.nopcommerce.com/how-a-blog-can-help-your-growing-e-commerce-business</link>
<title>How a blog can help your growing e-Commerce business</title>
<description><p>When and related comments.</p></description>
</item>
</channel>
</rss>
1.rss
step 1: Create a Blog Controller
\src\Presentation\Nop.Web\Controllers\BlogController.cs
public virtual IActionResult ListRss(string languageId = "")
{
var feed = new RssFeed(
string.Format("{0}: Blog", _storeContext.CurrentStore.Name),
"Blog",
new Uri(_webHelper.GetStoreLocation(false)),
DateTime.UtcNow);
if (!_blogSettings.Enabled)
return new RssActionResult(feed, _webHelper.GetThisPageUrl(false));
var items = new List<RssItem>();
var blogPosts = _blogService.GetAllBlogPosts(_storeContext.CurrentStore.Id.ToString(), languageId);
foreach (var blogPost in blogPosts)
{
// string blogPostUrl = Url.RouteUrl("BlogPost", new { SeName = blogPost.GetSeName() }, _webHelper.IsCurrentConnectionSecured() ? "https" : "http");
string blogPostUrl = string.Format("{0}/Blog/BlogPost/{1}/{2}", _webHelper.GetStoreLocation(false), blogPost.Id.ToString(), blogPost.SeName);
items.Add(new RssItem(blogPost.Title, blogPost.Body, new Uri(blogPostUrl), String.Format("urn:store:{0}:blog:post:{1}", _storeContext.CurrentStore.Id, blogPost.Id), blogPost.CreatedOnUtc));
}
feed.Items = items;
return new RssActionResult(feed, _webHelper.GetThisPageUrl(false));
}
Step 2: Create the RssActionResult class
The next step is to create a RssActionResult class that our MVC application can use to construct the RSS.
RssActionResult class override ContentResult class
path:\src\Presentation\Nop.Web.Framework\Mvc\RssActionResult.cs
namespace Nop.Web.Framework.Mvc
{
public class RssActionResult : ContentResult
{
/// <summary>
/// Ctor
/// </summary>
/// <param name="feed">Syndication feed</param>
/// <param name="feedPageUrl">Feed page url for atom self link</param>
public RssActionResult(RssFeed feed, string feedPageUrl)
{
this.ContentType = "application/atom+xml";
this.Feed = feed;
//add atom namespace
XNamespace atom = "http://www.w3.org/2005/Atom";
this.Feed.AttributeExtension = new KeyValuePair<XmlQualifiedName, string>(new XmlQualifiedName("atom", XNamespace.Xmlns.NamespaceName), atom.NamespaceName);
//add atom:link with rel='self'
this.Feed.ElementExtensions.Add(new XElement(atom + "link", new XAttribute("href", new Uri(feedPageUrl)), new XAttribute("rel", "self"), new XAttribute("type", "application/rss+xml")));
}
public RssFeed Feed { get; set; }
public override Task ExecuteResultAsync(ActionContext context)
{
Content = Feed.GetContent();
return base.ExecuteResultAsync(context);
}
public override void ExecuteResult(ActionContext context)
{
Content = Feed.GetContent();
base.ExecuteResult(context);
}
}
}
http://localhost:6999/common/RobotsTextFile
User-agent: *
Allow: /
User-agent: MJ12bot
Disallow: /
step 2: register router
http://localhost:6999/Robots.txt
User-agent: *
Allow: /