Rss的浏览器之痛
好久没写东西了,就写写最近遇到的问题吧!几天前写了一个关于Rss feed的code。其实说来这种东西在网上一搜一堆一堆,没有什么难度可言。
可就是这不可能出现的问题,产生了问题。
在写好代码后,在Ie7 和Ie8 下显示正常(ie7后集成了rss功能)。本以为这样就大功告成,当准备收工的时候,测试组说Rss在firefox下显示有问题。
赶快抓紧时间调整,然后放到firefox下问题依然存在。在网上找些大的网站,比对Rss的格式也没发现什么问题。花了一段时间后,无意间发现在写Rss头时,
link 是相对路径如“/home.aspx”,这样在ie7和ie8下是可以被识别的,可是在firefox下是无法识别的。所以,在写Rss头时,link 一定要写绝对路径如:
“http://localhost:8034/rsss.aspx”。以下附上一段写rss的简单代码。
可就是这不可能出现的问题,产生了问题。
在写好代码后,在Ie7 和Ie8 下显示正常(ie7后集成了rss功能)。本以为这样就大功告成,当准备收工的时候,测试组说Rss在firefox下显示有问题。
赶快抓紧时间调整,然后放到firefox下问题依然存在。在网上找些大的网站,比对Rss的格式也没发现什么问题。花了一段时间后,无意间发现在写Rss头时,
link 是相对路径如“/home.aspx”,这样在ie7和ie8下是可以被识别的,可是在firefox下是无法识别的。所以,在写Rss头时,link 一定要写绝对路径如:
“http://localhost:8034/rsss.aspx”。以下附上一段写rss的简单代码。
private const String HTTP = "http://";
protected void LoadData()
{
var context = HttpContext.Current;
var writer = new XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8);
WriteRSSPrologue(writer);
var homeUrl = HTTP+Request.ServerVariables["HTTP_HOST"] + "/home.aspx"; //注意问题有可能出现的地方
WriteRSSHeadChennel(writer,homeUrl);
var nodeList = GetAllRssNodeList();
foreach (var item in nodeList)
{
var doc = new Document(item.Id);
var description = doc.getProperty("Bodytext").Value.ToString();
AddRSSItem(writer, item.UpdateDate.ToString("R"), item.Parent.Name, item.Name, HTTP + Request.ServerVariables["HTTP_HOST"] + umbraco.library.NiceUrl(item.Id), description);
}
writer.Flush();
writer.Close();
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.ContentType = "text/xml";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.End();
}
private XmlTextWriter WriteRSSPrologue(XmlTextWriter writer)
{
writer.WriteStartDocument();
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
writer.WriteAttributeString("xmlns:content", "http://purl.org/rss/1.0/modules/content/");
writer.WriteAttributeString("xmlns:wfw", "http://wellformedweb.org/CommentAPI/");
writer.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");
return writer;
}
private XmlTextWriter WriteRSSHeadChennel(XmlTextWriter writer,string homeUrl)
{
writer.WriteStartElement("channel");
writer.WriteElementString("title", "MeubelTrack - Rss");
writer.WriteElementString("copyright", "Copyright (c) 2009,www.tribal.cn");
writer.WriteElementString("link", homeUrl);
writer.WriteElementString("generator", "Tribal");
writer.WriteElementString("description", "Meubeltrack.nl - dé route naar uw meubels");
return writer;
}
private XmlTextWriter AddRSSItem(XmlTextWriter writer, string pubDate,string sItemCategory, string sItemTitle, string sItemLink, string sItemDescription)
{
writer.WriteStartElement("item");
writer.WriteElementString("title", sItemTitle);
writer.WriteElementString("link", sItemLink);
writer.WriteElementString("description", HttpUtility.HtmlDecode(sItemDescription));
writer.WriteElementString("pubDate", pubDate);
writer.WriteElementString("category", sItemCategory);
writer.WriteElementString("author", "tribalChina");
writer.WriteEndElement();
return writer;
}
protected void LoadData()
{
var context = HttpContext.Current;
var writer = new XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8);
WriteRSSPrologue(writer);
var homeUrl = HTTP+Request.ServerVariables["HTTP_HOST"] + "/home.aspx"; //注意问题有可能出现的地方
WriteRSSHeadChennel(writer,homeUrl);
var nodeList = GetAllRssNodeList();
foreach (var item in nodeList)
{
var doc = new Document(item.Id);
var description = doc.getProperty("Bodytext").Value.ToString();
AddRSSItem(writer, item.UpdateDate.ToString("R"), item.Parent.Name, item.Name, HTTP + Request.ServerVariables["HTTP_HOST"] + umbraco.library.NiceUrl(item.Id), description);
}
writer.Flush();
writer.Close();
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.ContentType = "text/xml";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.End();
}
private XmlTextWriter WriteRSSPrologue(XmlTextWriter writer)
{
writer.WriteStartDocument();
writer.WriteStartElement("rss");
writer.WriteAttributeString("version", "2.0");
writer.WriteAttributeString("xmlns:content", "http://purl.org/rss/1.0/modules/content/");
writer.WriteAttributeString("xmlns:wfw", "http://wellformedweb.org/CommentAPI/");
writer.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");
return writer;
}
private XmlTextWriter WriteRSSHeadChennel(XmlTextWriter writer,string homeUrl)
{
writer.WriteStartElement("channel");
writer.WriteElementString("title", "MeubelTrack - Rss");
writer.WriteElementString("copyright", "Copyright (c) 2009,www.tribal.cn");
writer.WriteElementString("link", homeUrl);
writer.WriteElementString("generator", "Tribal");
writer.WriteElementString("description", "Meubeltrack.nl - dé route naar uw meubels");
return writer;
}
private XmlTextWriter AddRSSItem(XmlTextWriter writer, string pubDate,string sItemCategory, string sItemTitle, string sItemLink, string sItemDescription)
{
writer.WriteStartElement("item");
writer.WriteElementString("title", sItemTitle);
writer.WriteElementString("link", sItemLink);
writer.WriteElementString("description", HttpUtility.HtmlDecode(sItemDescription));
writer.WriteElementString("pubDate", pubDate);
writer.WriteElementString("category", sItemCategory);
writer.WriteElementString("author", "tribalChina");
writer.WriteEndElement();
return writer;
}