【问题标题】:itunes rss with c# MVC使用 c# MVC 的 iTunes RSS
【发布时间】:2017-11-15 20:21:17
【问题描述】:

大家好,我正在尝试使用 asp.net MVC 为 itunes 创建一个实时更新的 RSS 提要。我似乎无法验证提要,当您导航到该页面时,xml 的格式与我使用 webforms 时的格式不同。

这是视图

            @model IEnumerable<lofbc.org.Models.media>

            @{
                Layout = null;
            }

            <?xml version="1.0" encoding="UTF-8" ?>

            <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:itunesu="http://www.itunesu.com/feed" version="2.0">

                <channel>
                    <title>Life of Faith Bible Church Podcast</title>
                    <link>@Request.Url.Host</link>
                    <description>Welcome to Life of Faith Bible Church! We are dedicated to bringing you life-changing resources through our podcast and audio messages. You will find a wealth of material that will help you triumph in every area of life.</description>
                    <language>en-us</language>
                    <copyright>New Life Media copyright @DateTime.Now.Year</copyright>
                    <atom:link href="https://www.lofbc.org/rss" rel="self" type="application/rss+xml" />

                    <!--itunes specific-->
                    <itunes:author>Life of Faith Bible Church</itunes:author>
                    <itunes:summary>Welcome to Life of Faith Bible Church! We are dedicated to bringing you life-changing resources through our podcast and audio messages. You will find a wealth of material that will help you triumph in every area of life.</itunes:summary>
                    <itunes:owner>
                        <itunes:name>Life of Faith Bible Church</itunes:name>
                        <itunes:email>justin@lofbc.org</itunes:email>
                    </itunes:owner>
                    <itunes:explicit>No</itunes:explicit>
                    <itunes:image href="http://www.lofbc.org/rss/artwork/artwork.jpg" />
                    <itunes:category text="Religion & Spirituality"> </itunes:category>

                    @foreach (var item in Model)
                        {
                            var date = item.createdDate.ToString("yyyy");
                            var path = item.audioPath.ToString().Replace(".f4v", ".mp3");

                        <item>
                            <title>@item.title</title>
                            <url>http://lofbc-media.s3.amazonaws.com/@date/Audio/mp3/@path</url>
                            <pubDate>@item.createdDate.ToString("MMM dd, yyyy")</pubDate>
                            <description>@item.description</description>
                            <enclosure url="http://lofbc-media.s3.amazonaws.com/@item.createdDate.ToString("yyyy"))/Audio/mp3/@item.audioPath.ToString().Replace(".f4v", ".mp3")" length=""  type="audio/mpeg" />


                            <itunes:summary>@item.description</itunes:summary>
                        </item>
                    }
                </channel>
            </rss>

这是控制器

            using lofbc.org.context;
            using lofbc.org.Models;
            using System;
            using System.Collections.Generic;
            using System.Data.SqlClient;
            using System.Linq;
            using System.Web;
            using System.Web.Mvc;

            namespace lofbc.org.Controllers
            {
                public class rssController : Controller
                {
                    // GET: rss
                    public ActionResult Index()
                    {
                        using (ServiceArchivesEntities db = new ServiceArchivesEntities())
                        {
                            try
                            {
                                DateTime startDate = DateTime.Parse("01/01/2015 00:00:00");

                                var MT = (from a in db.tbl_media
                                          join b in db.tbl_media_ministers
                                          on a.MinisterID equals b.ID
                                          select new media
                                          {
                                              id = a.ID,
                                              createdDate = (DateTime)a.CreatedDate,
                                              title = a.Title,
                                              minister = b.Title + " " + b.FirstName + " " + b.LastName,
                                              audioPath = a.PathToAudio,
                                              videoPath = a.PathToVideo,
                                              views = a.Views,
                                              listens = a.Listens,
                                              status = a.Status
                                          }).Where(t => t.status == 2 && t.createdDate >= startDate).OrderByDescending(t => t.createdDate).Take(100).ToList();

                                return View(MT);
                            }
                            catch (SqlException ex)
                            {
                                Response.Write(ex.ToString());
                                return View();
                            }

                        }
                    }
                }
            }

现场演示http://development.lofbc.org/rss

【问题讨论】:

    标签: c# asp.net-mvc rss


    【解决方案1】:
            <itunes:category text="Religion & Spirituality"> </itunes:category>
    

    应该是

        <itunes:category text="Religion &amp; Spirituality"> </itunes:category>
    

    iTunes 也可能反对&lt;?xml 之前的额外空行。将 Razor 代码尽可能紧凑地打包在页面顶部:

    @model IEnumerable<lofbc.org.Models.media>
    @{  Layout = null;  }
    <?xml version="1.0" encoding="UTF-8" ?>
    <rss xmlns:atom="http://www.w3.org/2005/Atom"   ....
    

    【讨论】:

    • 谢谢我试过了,结果还是一样。页面是否有可能以 html 而不是 xml 的形式拉动?我使用验证器时收到此错误此提要未验证。第 2 行,第 0 列:XML 解析错误::2:0: XML or text declaration not at start of entity help center 另外,通过实施以下建议,可以提高与最广泛的提要阅读器的互操作性。 Feed 不应使用“text/html”媒体类型提供
    • 这个错误听起来好像你在开始时仍然有一个杂散的空白行。处理 text/html 在这里解决:stackoverflow.com/questions/971806/…
    • 感谢您的帮助,链接中的信息更正了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    • 2016-03-29
    • 2012-04-15
    • 2013-03-03
    • 1970-01-01
    • 2012-04-16
    相关资源
    最近更新 更多