<%@ WebHandler Language="C#" Class="VideoFaq.robotshandler" %> using System; using System.Web; using System.Configuration; using System.Collections.Generic; using System.Xml; using App_Code; using System.IO; namespace VideoFaq { public class robotshandler: IHttpHandler { private static readonly string RobotsProduction = ConfigurationManager.AppSettings["RobotsProduction"].ToString(); public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write("User-agent: *\r\n"); if ((!(ProjectUtility.IsIframedVersion(context))) && (ProjectUtility.IsRobotsProduction(context)) && (context.Request.RawUrl.IndexOf("robots.txt", StringComparison.InvariantCultureIgnoreCase) > -1)) { //Valuto se il dominio corrente è aperto ai robots oppure no: bool DomainIsOpen = false; foreach (KeyValuePair pair in StaticObjects.DomainsTableForRobots) { if (HttpContext.Current.Request.Url.ToString().ToLower().IndexOf(pair.Key.ToLower()) > -1) { DomainIsOpen = pair.Value == "Y" ? true : false; break; } } if (DomainIsOpen) { PrintBlockedURLs(context, ProjectUtility.GetCurrentApplicationPath()); context.Response.Write("Allow: /\r\n"); } else { context.Response.Write("Disallow: /\r\n"); } } else if (context.Request.RawUrl.IndexOf("robots.txt") > -1) { context.Response.Write("Disallow: /\r\n"); } } public bool IsReusable { get { return false; } } private void PrintBlockedURLs(HttpContext context, string actualDomain) { //DEBUG ONLY: //actualDomain = "http://videofaq.costakreuzfahrten.de/"; //STAMPA DEGLI URL BLOCCATI XmlDocument doc = new XmlDocument(); string xmlFile = context.Server.MapPath("~/ResourcesXml/URLsFilter.xml"); if (File.Exists(xmlFile)) { try { doc.Load(xmlFile); } catch (Exception ex) { throw new FileLoadException("Error parsing the XML file", xmlFile, ex); } } XmlNodeList nodes = doc.SelectNodes("/Links"); if (nodes.Count > 0) { XmlNodeList childs = nodes[0].ChildNodes; foreach (XmlNode node in childs) { //Considero solo quelli del dominio corrente: if (node.InnerText.IndexOf(actualDomain) > -1) { context.Response.Write("Disallow: " + node.InnerText.Replace(actualDomain, "/") + "\r\n"); } } } } } }