← Tutti gli articoli

Asp.Net Generic Handler (ashx) to generate Google Sitemap

20 August 2010  ·  Asp.Net · Article  ·  1229 visite

Asp.Net Generic Handler (ashx) to generate Google Sitemap

<%@ WebHandler Language="C#" Class="SiteMap" %>

using System;
using System.Linq;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.Text;
using System.IO;

using MaragnaNetModel;

    public class SiteMap : IHttpHandler
    {
        private XmlWriter _xmlWriter;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/xml";
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;
            settings.Indent = true;
            _xmlWriter = XmlWriter.Create(context.Response.OutputStream, settings);
            _xmlWriter.WriteStartDocument();
            _xmlWriter.WriteStartElement("urlset", "http://www.google.com/schemas/sitemap/0.9");
            // Add root node
            AddUrl("http://www.maragna.net", DateTime.Now, "weekly", "1.0");
            AddUrl("http://maragna.net/Showcase.aspx", DateTime.Now, "weekly", "1.0");
            AddUrl("http://maragna.net/ContentSearch.aspx", DateTime.Now, "weekly", "1.0");

            // Add all other nodes

            MaragnaNetEntities ctx = new MaragnaNetEntities(ConfigurationManager.ConnectionStrings["MaragnaNetEntities"].ToString());
            var contenuti = from h in ctx.ContentsHistory
                            where h.ContentStatusID == ContentStatus.Published && h.FinishDate == null
                            select new
                                {
                                    h.BeginDate,
                                    //h.Contents.ContentTitle,
                                    //h.Contents.ContentDescription
                                    h.ContentId
                                };
            foreach(var c in contenuti.ToList()){
                AddUrl("http://maragna.net/ContentView.aspx?ContentId=" + c.ContentId.ToString(), c.BeginDate,"monthly","1.0");
            }






            _xmlWriter.WriteEndElement();
            _xmlWriter.WriteEndDocument();

            _xmlWriter.Flush();
        }
        private void AddUrl(string Url, DateTime lastmod, string changefreq, string priority)
        {
            // Skip empty Urls
            if (String.IsNullOrEmpty(Url))
                return;
            // Skip remote nodes

            // Open url tag
            _xmlWriter.WriteStartElement("url");
            // Write location
            _xmlWriter.WriteStartElement("loc");
            _xmlWriter.WriteString(GetFullUrl(Url));
            _xmlWriter.WriteEndElement();
            // Write last modified
            _xmlWriter.WriteStartElement("lastmod");
            _xmlWriter.WriteString(String.Format("{0:yyyy-MM-dd}", lastmod));
            _xmlWriter.WriteEndElement();

            // Write changefreq
            _xmlWriter.WriteStartElement("changefreq");
            _xmlWriter.WriteString(changefreq);
            _xmlWriter.WriteEndElement();

            // Write priority
            _xmlWriter.WriteStartElement("priority");
            _xmlWriter.WriteString(priority);
            _xmlWriter.WriteEndElement();

            // Close url tag
            _xmlWriter.WriteEndElement();
        }
        private string GetFullUrl(string url)
        {
            HttpContext context = HttpContext.Current;
            string server = context.Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.UriEscaped);
            return Combine(server, url);
        }
        private string Combine(string baseUrl, string url)
        {
            baseUrl = baseUrl.TrimEnd(new char[] { '/' });
            url = url.TrimStart(new char[] { '/' });
            return baseUrl + "/" + url;
        }
        private string GetLastModified(string url)
        {
            HttpContext context = HttpContext.Current;
            string physicalPath = context.Server.MapPath(url);
            return File.GetLastWriteTimeUtc(physicalPath).ToString("s") + "Z";
        }
        public bool IsReusable
        {
            get { return true; }
        }

    }

Si è verificato un errore imprevisto. Ricarica

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.