简体   繁体   English

如何在asp.net中生成动态站点地图?

[英]How to generate dynamic sitemap in asp.net?

I am trying to build up a sitemap for all my dynamic pages. 我正在尝试为我所有的动态页面建立一个站点地图。 I have tried generating it by using web form and writing code behind function. 我试图通过使用Web表单并在函数后面编写代码来生成它。 But on submitting the page on Google Webmaster it is giving me an error- 1. We encountered an error while trying to access your sitemap. 但是在Google网站站长上提交页面时,这给我一个错误-1.尝试访问您的站点地图时遇到错误。 Please ensure your sitemap follows our guidelines and can be accessed at the location you provided and then resubmit. 请确保您的站点地图符合我们的指南,并且可以在您提供的位置进行访问,然后重新提交。 2. Sitemap is HTML- Your sitemap appears to be an HTML page. 2. Sitemap是HTML-您的站点地图似乎是HTML页面。 please use a supported sitemap format instead. 请改用支持的站点地图格式。 This what I was trying- 这就是我正在尝试的

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();
                }
            }
        }
    }

And my aspx page is- 我的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>

Please guide me where I am doing wrong.? 请指导我哪里做错了。

Your aspx page clearly states <html …> as first element. 您的aspx页面清楚地将<html …>为第一个元素。

I think you need to delete everything in your aspx file except line 1. You need to make sure that the sitemap is reachable at domain/sitemap.xml. 我认为您需要删除aspx文件中除第1行以外的所有内容。您需要确保可以在domain / sitemap.xml上访问该站点地图。

In case you need further help, please download and provide the sitemap.xml file. 如果您需要进一步的帮助,请下载并提供sitemap.xml文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM