Most Popular

Thứ Bảy, 7 tháng 11, 2015

Tạo sitemap.xml trong asp.net

Làm thế nào  để tạo ra file sitemap.xml động mà rất hữu ích cho công cụ tìm kiếm để tìm tất cả các link của bạn ?

Google công bố hướng dẫn rất hữu ích cho các chủ sở hữu trang web, bao gồm chi tiết về đặc điểm kỹ thuật của các định dạng sitemap rằng họ hài lòng để chấp nhận. Các tiêu chuẩn hiện nay là Nghị định thư SiteMap 0.9, và là định nghĩa của một tập tin xml khá đơn giản. Một cách để tạo ra được bằng tay, nhưng làm như vậy có nghĩa là tôi sẽ phải cập nhật nó mỗi khi tôi thêm một bài báo, hoặc thậm chí xóa một. Vì vậy, giống như RSS feed hiện tại của tôi, tôi đã quyết định để tạo ra sitemap tự động theo yêu cầu.

Các định dạng xml đơn giản là:




Sitemap.xml
Đối với mỗi tag <url>, thì phải có một thẻ <loc> chỉ vào URL của trang. <Lastmod>, <changefreq> và <priority> là tùy chọn.

Bước 1.

Tạo một file sitemap.xml Trống vào thư mục gốc của thư mục web của bạn. Và thêm vào kiểu nội dung như hiển thị bên dưới.

Tạo file sitemap.xml
Bước 2.

Thêm trang .aspx mới vào ứng dụng của bạn và đặt tên cho nó là “sitemap.aspx”.

Bước 3.

Viết mã dưới đây vào sự kiện load của “sitemap.aspx” trang. Dưới đây là code đầy đủ sitemap.aspx.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;
using System.Data.SqlClient;
using System.Data;
public partial class sitemap : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();
        Response.ContentType = “text/xml”;
        using (XmlTextWriter writer = new XmlTextWriter(Server.MapPath(“sitemap.xml”), Encoding.UTF8))
        {
            writer.WriteStartDocument();
            writer.WriteStartElement(“urlset”);
            writer.WriteAttributeString(“xmlns”, “http://www.google.com/schemas/sitemap/0.84”);
            writer.WriteAttributeString(“xmlns:xsi”, “http://www.w3.org/2001/XMLSchema-instance”);
            writer.WriteAttributeString(“xsi:schemaLocation”, “http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd”);
            string dbstring = System.Configuration.ConfigurationManager.ConnectionStrings[“database_con”].ConnectionString;
            SqlConnection connection = new SqlConnection(dbstring);
            connection.Open();
            SqlDataAdapter adp1 = new SqlDataAdapter(“Select title,id from articles”, connection);
            DataSet ds = new DataSet();
            adp1.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int LIntCtr = 0; LIntCtr <= ds.Tables[0].Rows.Count – 1; LIntCtr++)
                {
                    writer.WriteStartElement(“url”);
                    writer.WriteElementString(“loc”, “http://www.mysite.com/articles/” + ds.Tables[0].Rows[LIntCtr][“title”].ToString()+ “”);
                    writer.WriteElementString(“lastmod”, String.Format(“{0:yyyy-MM-dd}”, DateTime.Now));
                    writer.WriteElementString(“changefreq”, “daily”);
                    writer.WriteElementString(“priority”, “1.00”);
                    writer.WriteEndElement();
                }
            }
            writer.WriteEndElement();
            writer.Flush();
        }
    }
}
Bước 4.

Chạy “sitemap.aspx” trang của bạn.

Bước 5.

Kiểm tra “sitemap.xml” của file.Nó đã sẵn sàng cho bạn

Xem sitemap.xml
Vậy là chúng ta đã tạo được sitemap trong lập trình asp rồi. Nếu có bất kỳ thắc mắc nào hãy để lại comment dưới đây nhé.
Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 nhận xét

 
© 2011 Share Tài Liệu
Posts RSSComments RSS
Back to top