在asp.net core 1.0中创建RSS feed

编程入门 行业动态 更新时间:2024-10-12 14:22:46
本文介绍了在asp core 1.0中创建RSS feed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在Asp Core 1.0 MVC 6中工作 我正在尝试编写一个组件来提供我网站上的RSS提要.

I am working in Asp Core 1.0 MVC 6 I am trying to write a component to provide RSS feeds from my websites.

我找到了这篇文章表明System.ServiceModel.Syndication尚未移植到ASP.NET CORE.

I found this post that suggests that System.ServiceModel.Syndication has yet to be ported to ASP.NET CORE.

我无法针对整个.NET框架.

I am unable to target the full .NET framework.

建议将其编写为xml解析器.但是,我一直在努力应对可能需要的一切.

The suggestion is to write as an xml parser. I am however struggling to get my head around everything that might be required.

我已经构建了将数据转换为XML的功能,但是现在需要更好地了解如何从IActionResult调用它(或者实际上是如何生成可以放在我的页面上的链接).

I have built the functionality to get my data into XML but now need to better understand how to allow this to be called from an IActionResult (or indeed how to generate a link that may be placed on my page).

我可以提供代码示例,但不确定是否会有所帮助. 有谁能指出我实现这些目标的正确方向?

I can provide samples of my code but am not sure it'll be helpful. Is anyone able to point me in the right direction of examples achieving this?

我也在这篇文章中找到了答案,这些答案指出了一些想法,但我认为这是MVC6/Asp Core之前的版本: ASP.NET MVC中的RSS提要

I also found an answer on this post which points towards some ideas but I think is pre MVC6/Asp Core: RSS Feeds in ASP.NET MVC

推荐答案

// action to return the feed [Route("site/GetRssFeed/{type}")] public IActionResult GetRssFeed(ArticleStatusTypes type) { var feed = _rss.BuildXmlFeed(type); return Content(feed, "text/xml"); } public string BuildXmlFeed(ArticleStatusTypes type) { var key = $"RssFeed{Convert.ToInt32(type)}{_appInfo.ApplicationId}"; var articles = _cache.GetCachedData(key) ?? _cache.SetCache(key, _service.GetItems(Convert.ToInt32(type), _appInfo.CacheCount)); StringWriter parent = new StringWriter(); using (XmlTextWriter writer = new XmlTextWriter(parent)) { writer.WriteProcessingInstruction("xml-stylesheet", "title=\"XSL_formatting\" type=\"text/xsl\" href=\"/skins/default/controls/rss.xsl\""); writer.WriteStartElement("rss"); writer.WriteAttributeString("version", "2.0"); writer.WriteAttributeString("xmlns:atom", "www.w3/2005/Atom"); // write out writer.WriteStartElement("channel"); // write out -level elements writer.WriteElementString("title", $"{_appInfo.ApplicationName} {type}" ); writer.WriteElementString("link", _appInfo.WebsiteUrl); //writer.WriteElementString("description", Description); writer.WriteElementString("ttl", "60"); writer.WriteStartElement("atom:link"); //writer.WriteAttributeString("href", Link + Request.RawUrl.ToString()); writer.WriteAttributeString("rel", "self"); writer.WriteAttributeString("type", "application/rss+xml"); writer.WriteEndElement(); if (articles != null) { foreach (var article in articles) { writer.WriteStartElement("item"); writer.WriteElementString("title", article.Title); writer.WriteElementString("link", _appInfo.WebsiteUrl); // todo build article path writer.WriteElementString("description", article.Summary); writer.WriteEndElement(); } } // write out writer.WriteEndElement(); // write out writer.WriteEndElement(); } return parent.ToString(); }

更多推荐

在asp.net core 1.0中创建RSS feed

本文发布于:2023-11-06 08:25:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563185.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:core   net   asp   feed   RSS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!