Popular blog tags

NopCommerce 4.x (4)-How to Generate a rss in ASP.NET Core


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>
        <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" />
            <guid isPermaLink="false">urn:store:1:blog:post:2</guid>
            <title>Why your online store needs a wish list</title>
            <description>&lt;p&gt;What canswer to ure.&lt;/p&gt;</description>
            <guid isPermaLink="false">urn:store:1:blog:post:1</guid>
            <title>How a blog can help your growing e-Commerce business</title>
            <description>&lt;p&gt;When and related comments.&lt;/p&gt;</description>



step 1: Create a Blog Controller


   public virtual IActionResult ListRss(string languageId = "")
            var feed = new RssFeed(
                string.Format("{0}: Blog", _storeContext.CurrentStore.Name),
                new Uri(_webHelper.GetStoreLocation(false)),

            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


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();




User-agent: *
Allow: /
User-agent: MJ12bot
Disallow: /

step 2: register router



User-agent: *
Allow: /