Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Zac 239 posts 541 karma points
    Jun 27, 2013 @ 16:48
    Zac
    0

    Source code?

    Would you be willing to share the source code for this project with us?  
    We're looking to implement a dynamic robots.txt file but it would need to handle some special use cases.

    Thanks

    Zac

  • Sebastiaan Janssen 5061 posts 15544 karma points MVP admin hq
    Jun 28, 2013 @ 08:56
    Sebastiaan Janssen
    1

    Hey Zac,

    Looks like I kinda lost the source code for this, but not to worry, a little decompile helped, this is all there is to it: 

    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("");             }         }     } }
  • Zac 239 posts 541 karma points
    Jun 28, 2013 @ 14:50
    Zac
    0

    Thanks!

  • serhio 22 posts 82 karma points
    Apr 16, 2015 @ 12:10
    serhio
    0

    if a site is multilingual, like "mysite.com/de", what should be instead of "HttpContext.Current.Request.ServerVariables["HTTP_HOST"]"?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies