Skip to content

Instantly share code, notes, and snippets.

@wiedikerli
Created February 22, 2016 10:22
Show Gist options
  • Save wiedikerli/ab0b874de349f264758d to your computer and use it in GitHub Desktop.
Save wiedikerli/ab0b874de349f264758d to your computer and use it in GitHub Desktop.
Dynamic Robots.txt => Sitemap: http://{HTTP_HOST}/sitemap source code from this package https://our.umbraco.org/projects/website-utilities/cultiv-dynamicrobots
using System;
using System.IO;
using System.Web;
namespace Cultiv.DynamicRobots
{
public class RobotsTxt : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
var path = HttpContext.Current.Server.MapPath(VirtualPathUtility.ToAbsolute("~/robots.txt"));
if (File.Exists(path))
{
var streamReader = File.OpenText(path);
var text = streamReader.ReadToEnd();
context.Response.Write(text.Replace("{HTTP_HOST}", HttpContext.Current.Request.ServerVariables["HTTP_HOST"]));
streamReader.Close();
streamReader.Dispose();
}
else
{
context.Response.Write("");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment