Kanały RSS w ASP.NET MVC
Jak polecasz obsługę kanałów RSS w ASP.NET MVC? Korzystanie z biblioteki innej firmy? Korzystanie z RSS rzeczy w BCL? Po prostu Tworzenie widoku RSS, który renderuje XML? Albo coś zupełnie innego?
5 answers
Oto co polecam:
- utworzyć klasę o nazwie RssResult, która dziedziczy z abstrakcyjnej klasy bazowej / Align = "left" /
- nadpisuje metodę ExecuteResult.
- ExecuteResult ma Kontrolercontext przekazany do niego przez wywołującego i dzięki temu można uzyskać typ danych i treści.
Po zmianie typu treści na rss, będziesz chciał serializować dane do RSS (za pomocą własnego kodu lub innej biblioteki) i napisać do odpowiedź.
Utwórz akcję na kontrolerze, do którego chcesz zwrócić rss i ustaw typ powrotu jako rssresult. Pobierz dane z modelu w oparciu o to, co chcesz zwrócić.
Wtedy każde żądanie tego działania otrzyma rss dowolnych danych, które wybierzesz.
To chyba najszybszy i wielokrotnego użytku sposób zwracania rss ma odpowiedź na żądanie w ASP.NET MVC.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-08-15 03:18:50
. NET Framework wyświetla klasy obsługujące syndation: SyndicationFeed itp. Więc zamiast renderować samodzielnie lub używając innej sugerowanej biblioteki RSS, dlaczego nie pozwolić frameworkowi się tym zająć?
W zasadzie wystarczy wykonać następujące działania niestandardowe i Gotowe:
public class RssActionResult : ActionResult
{
public SyndicationFeed Feed { get; set; }
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ContentType = "application/rss+xml";
Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed);
using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
{
rssFormatter.WriteTo(writer);
}
}
}
Teraz w akcji kontrolera możesz po prostu zwrócić:
return new RssActionResult() { Feed = myFeedInstance };
Jest pełna próbka na moim blogu w http://www.developerzen.com/2009/01/11/aspnet-mvc-rss-feed-action-result/
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-06-15 05:38:07
Zgadzam się z Haacked. Obecnie wdrażam moją stronę / bloga za pomocą frameworka MVC i wybrałem proste podejście do tworzenia nowego widoku dla RSS:
<%@ Page ContentType="application/rss+xml" Language="C#" AutoEventWireup="true" CodeBehind="PostRSS.aspx.cs" Inherits="rr.web.Views.Blog.PostRSS" %><?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>ricky rosario's blog</title>
<link>http://<%= Request.Url.Host %></link>
<description>Blog RSS feed for rickyrosario.com</description>
<lastBuildDate><%= ViewData.Model.First().DatePublished.Value.ToUniversalTime().ToString("r") %></lastBuildDate>
<language>en-us</language>
<% foreach (Post p in ViewData.Model) { %>
<item>
<title><%= Html.Encode(p.Title) %></title>
<link>http://<%= Request.Url.Host + Url.Action("ViewPostByName", new RouteValueDictionary(new { name = p.Name })) %></link>
<guid>http://<%= Request.Url.Host + Url.Action("ViewPostByName", new RouteValueDictionary(new { name = p.Name })) %></guid>
<pubDate><%= p.DatePublished.Value.ToUniversalTime().ToString("r") %></pubDate>
<description><%= Html.Encode(p.Content) %></description>
</item>
<% } %>
</channel>
</rss>
Aby uzyskać więcej informacji, sprawdź (shameless plug) http://rickyrosario.com/blog/creating-an-rss-feed-in-asp-net-mvc
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-30 02:25:24
Innym szalonym podejściem, ale ma swoją zaletę, jest użycie normalnego .widok aspx do renderowania RSS. W metodzie akcji Ustaw odpowiedni typ zawartości. Jedną z zalet tego podejścia jest to, że łatwo jest zrozumieć, co jest renderowane i jak dodać niestandardowe elementy, takie jak geolokalizacja.
Z drugiej strony inne wymienione podejścia mogą być lepsze, po prostu ich nie używałem. ;)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-08-15 05:49:33
Dostałem to z Eran Kampf i Scott Hanselman vid (zapomniałem linku), więc to tylko trochę różni się od niektórych innych postów tutaj, ale mam nadzieję, że pomocne i skopiować wklej gotowy jako przykładowy kanał rss.
using System;
using System.Collections.Generic;
using System.ServiceModel.Syndication;
using System.Web;
using System.Web.Mvc;
using System.Xml;
namespace MVC3JavaScript_3_2012.Rss
{
public class RssFeed : FileResult
{
private Uri _currentUrl;
private readonly string _title;
private readonly string _description;
private readonly List<SyndicationItem> _items;
public RssFeed(string contentType, string title, string description, List<SyndicationItem> items)
: base(contentType)
{
_title = title;
_description = description;
_items = items;
}
protected override void WriteFile(HttpResponseBase response)
{
var feed = new SyndicationFeed(title: this._title, description: _description, feedAlternateLink: _currentUrl,
items: this._items);
var formatter = new Rss20FeedFormatter(feed);
using (var writer = XmlWriter.Create(response.Output))
{
formatter.WriteTo(writer);
}
}
public override void ExecuteResult(ControllerContext context)
{
_currentUrl = context.RequestContext.HttpContext.Request.Url;
base.ExecuteResult(context);
}
}
}
I Kod kontrolera....
[HttpGet]
public ActionResult RssFeed()
{
var items = new List<SyndicationItem>();
for (int i = 0; i < 20; i++)
{
var item = new SyndicationItem()
{
Id = Guid.NewGuid().ToString(),
Title = SyndicationContent.CreatePlaintextContent(String.Format("My Title {0}", Guid.NewGuid())),
Content = SyndicationContent.CreateHtmlContent("Content The stuff."),
PublishDate = DateTime.Now
};
item.Links.Add(SyndicationLink.CreateAlternateLink(new Uri("http://www.google.com")));//Nothing alternate about it. It is the MAIN link for the item.
items.Add(item);
}
return new RssFeed(title: "Greatness",
items: items,
contentType: "application/rss+xml",
description: String.Format("Sooper Dooper {0}", Guid.NewGuid()));
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-10 22:54:31