記録として、私はどの .NET よりも Java と RnR に精通しています。私はあちこちで関数のいくつかのバグを修正し、あちこちでいくつかの新しい「機能」をグーグル検索しましたが、それは私の専門知識ではありません。邪魔にならないように注意してください。これが私の問題です。ASP.NET で構築されたサイトの RSS フィードを作成する必要があります。Google はいくつかの解決策を見つけましたが、私はこれを例として使用しました: http://www.aspfree.com/c/a/C-Sharp/Creating-an-RSS-Feed-with-ASP-Net-Written-インCシャープ/
奇妙なことに、エラーは表示されずにすべて動作しますが、実行すると空白のページしか表示されません。SQL をテストしました。動作し、必要な行を返しますが、DB に接続しているかどうか、または何も返さない理由を診断する方法がわかりません。コードは次のとおりです。
asp.aspx.cs:
using System;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Xml;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
//Class for returning RSS feed Data
public partial class rss : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string sSource;
string sLog;
string sEvent;
sSource = "RSS Feed";
sLog = "Application";
sEvent = "Database Connectiont";
// Connect to the Database
SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=events;Persist Security Info=True;User ID=wa;Password=password;");
// Retrieve the SQL query results and bind it to the Repeater
const string SQL_QUERY = "SELECT EventName, Comments, URL, StartDate FROM event WHERE SaleDateTo > getdate() AND SaleDateFrom < getdate() AND redirecttourl is not null ORDER BY EventDate";
SqlCommand myCommand = new SqlCommand(SQL_QUERY, myConnection);
bool IsDbAvailable = true;
try {
myConnection.Open();
rptRSS.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
rptRSS.DataBind();
myConnection.Close();
}
catch {
IsDbAvailable = false;
if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource,sLog);
}
finally {
myConnection.Close();
}
}
protected string FormatForXML(object input)
{
string data = input.ToString(); //cast input to string
// replace those characters disallowed in XML documents
data = data.Replace("&", "&");
data = data.Replace("\"", """);
data = data.Replace("'", "'");
data = data.Replace("<", "<");
data = data.Replace(">", ">");
return data;
}
}
rss.aspx:
<%@ Page language="c#" CodeFile="rss.aspx.cs" ContentType="text/xml" AutoEventWireup="false" Inherits="rss"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<asp:Repeater id="rptRSS" runat="server">
<HeaderTemplate>
<rss version="2.0">
<channel>
<title>TA On-Sale Feed</title>
<link>http://www.website.com/rss.aspx</link>
<description>All events current on-sale</description>
</HeaderTemplate>
<itemTemplate>
<event>
<title><%#FormatForXML(DataBinder.Eval(Container.DataItem,"EventName"))%>
</title>
<description>
<%#FormatForXML(DataBinder.Eval(Container.DataItem, "Comments"))%>
</description>
<link>http://order.ticketalternative.com<%#DataBinder.Eval(Container.DataItem, "url")%>
</link>
<eventdate>
<%#DataBinder.Eval(Container.DataItem, "StartDate")%>
<eventdate>
</event>
</ItemTemplate>
<FooterTemplate>
</channel>
</rss>
</FooterTemplate>
</asp:Repeater>
</html>