C# Ashx Google xml 站点地图生成错误
我正在使用 ashx 为 Google 提供我的网站站点地图。一切都很完美,直到我最近。
在 Google 中请求站点地图时,网址为 http://www.naughtyfancydress.com/sitemap.ashx 我得到: XML 解析错误:格式不正确 位置:http://naughtyfancydress.com/sitemap.ashx 第 1 行,第 1 列:`I�%&/m�{J�
我在 ashx 中的精简代码如下所示:
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.ContentType = "text/xml";
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(3600));
context.Response.Cache.SetCacheability(HttpCacheability.Public);
var writer = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement("urlset");
writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");
writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
writer.WriteStartElement("url");
writer.WriteElementString("loc", "http://www.naughtyfancydress.com/");
writer.WriteElementString("changefreq", "daily");
writer.WriteElementString("priority", "1.0");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
欢迎任何有关如何解决问题的想法。
编辑:如果您在 Chrome 中检查上面的链接,则不会显示任何内容,我相信这是 Chrome 的问题,请使用 FireFox 检查链接。
I'm using a ashx to deliver my websites sitemap for Google. Its all worked perfectly until I recently.
When requesting the sitemap in Google at http://www.naughtyfancydress.com/sitemap.ashx I get:
XML Parsing Error: not well-formed
Location: http://naughtyfancydress.com/sitemap.ashx
Line Number 1, Column 1:`I�%&/m�{J�
My stripped down code in the ashx looks like:
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.ContentType = "text/xml";
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(3600));
context.Response.Cache.SetCacheability(HttpCacheability.Public);
var writer = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement("urlset");
writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");
writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
writer.WriteStartElement("url");
writer.WriteElementString("loc", "http://www.naughtyfancydress.com/");
writer.WriteElementString("changefreq", "daily");
writer.WriteElementString("priority", "1.0");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
Any ideas on how to resolve would be welcomed.
EDIT: If you check the link above in Chrome nothing will display, I believe this is a Chrome issue, please check the link with FireFox.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于其他人来说,问题是在 Global.asax 中的 Application_PreRequestHandlerExecute 方法中,我在内容中进行了 gzip 压缩。
这显然将内容编码从 utf-8 更改为 gzip,即使上面已指定。这就是修复方法,确保站点地图处理程序不会以 gzip 形式发送内容。
For anyone else, the issue was that in the Global.asax, in the Application_PreRequestHandlerExecute method I was gzip'in my content.
This obviously changes the content encoding to gzip from utf-8 even though it was specified above. That's the fix, ensure the sitemap handler doesn't send content as gzip.