1
我想爲我的所有動態頁面建立一個站點地圖。我曾嘗試通過使用Web窗體並在函數後面編寫代碼來生成它。但是,在Google網站管理員提交頁面時,它給了我一個錯誤 - 1.我們在嘗試訪問您的站點地圖時遇到錯誤。請確保您的站點地圖符合我們的準則,並可以在您提供的位置進行訪問,然後重新提交。 2. Sitemap是HTML - 您的站點地圖似乎是一個HTML頁面。請改用受支持的站點地圖格式。 這就是我所trying-如何在asp.net中生成動態站點地圖?
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "text/xml";
using (XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8))
{
writer.WriteStartDocument();
writer.WriteStartElement("urlset");
writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
writer.WriteStartElement("url");
writer.WriteElementString("loc","http://www.mywebsite.com/");
writer.WriteElementString("changefreq","weekly");
writer.WriteElementString("priority","1.0");
writer.WriteEndElement();
string connect = WebConfigurationManager.ConnectionStrings["connectionName"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connect))
{
using(SqlCommand cmd1=new SqlCommand("select attr1,attr2,substring(attr3,0,300) as attr31, attr4 from tblname order by attr1",conn))
{
cmd1.CommandType=CommandType.Text;
conn.Open();
using (SqlDataReader rdr1=cmd1.ExecuteReader())
{
while(rdr1.Read())
{
writer.WriteElementString("loc","http://www.mywebsite.com/page1.aspx");
if (rdr1[1] != DBNull.Value)
writer.WriteElementString("lastmod",String.Format("{0:yyyy-MM-dd}",rdr1[1]));
writer.WriteElementString("changefreq","daily");
writer.WriteElementString("priority","0.80");
writer.WriteEndElement();
}
rdr1.NextResult();
while (rdr1.Read())
{
writer.WriteElementString("loc","http://www.mywebsite.com/page2.aspx?id="+rdr1[0].ToString());
if (rdr1[1] != DBNull.Value)
writer.WriteElementString("lastmod",String.Format("{0:yyyy-MM-dd}",rdr1[1]));
writer.WriteElementString("changefreq","daily");
writer.WriteElementString("priority","0.80");
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
}
Response.End();
}
}
}
}
我的aspx頁面是 -
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sitemap.aspx.cs" Inherits="sitemap" %>
<!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">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
請指引我哪裏做錯了?
你可能想標記「asp.net」到這個問題 – TruongSinh
@TruongSinh編輯標籤部分。但是你對這個問題有什麼想法嗎? – Omi