Eric
Apr 27, 2015
  9898
(4 votes)

Simple robots.txt built with asp.net mvc

The other day there was a question about serving static files from episerver, like a robots.txt. Since most editor like to be able to update the robots.txt them self many of us has build solutions for that. There are also some packages in the nuget feed from EPiServer that will help you do just that.

But if you are like me, I do not like to have pre-compiled solutions from other companies in my project, some are better then others but still I like to be able to change most of my code in a project. Of course you can download the source code in many of those packages but some times they tend to do a bit more then you might expected as well.

 

In this case I just would like to serve a robots.txt if it is requested by a search engine for instance.

Solution

First I create the robots.txt controller. Since we are working with EPiServer CMS I have added a textarea on the startpage type and therefore editors are able to change the content of the file.

 

   1:      public class RobotsTxtController : Controller
   2:      {
   3:          private Injected<IContentLoader> ContentLoader { get; set; }
   4:   
   5:          [ContentOutputCache]
   6:          public ActionResult Index()
   7:          {
   8:              var startPage = ContentLoader.Service.Get<StartPage>(ContentReference.StartPage);
   9:   
  10:              string content = startPage.RobotsTxtContent;
  11:              return Content(content, "text/plain");
  12:          }
  13:      }

 

Nesx step is to create an initialization module were we map a route to our robots.txt file.

 

   1:      [InitializableModule]
   2:      [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
   3:      public class RenderingInitialization : IInitializableModule
   4:      {
   5:          public void Initialize(InitializationEngine context)
   6:          {
   7:       
   8:              //Maps route for robots.txt
   9:              RouteTable.Routes.MapRoute("RobotsTxtRoute", "robots.txt", new { controller = "RobotsTxt", action = "Index" });
  10:          }
  11:   
  12:   
  13:          public void Preload(string[] parameters) { }
  14:   
  15:          public void Uninitialize(InitializationEngine context)
  16:          {
  17:              //Add uninitialization logic
  18:   
  19:          }
  20:      }

 

And finally we add the property to our startpage model

        [UIHint(UIHint.Textarea)]
        [Display(Name = "Robots.txt", Order = 90, GroupName = Global.GroupNames.SiteSettings)]
        public virtual string RobotsTxtContent { get; set; }
Apr 27, 2015

Comments

Marija Jemuovic
Marija Jemuovic Apr 27, 2015 09:57 AM

Nice idea!

Eric
Eric Apr 27, 2015 04:26 PM

Thanks.

valdis
valdis Apr 28, 2015 12:30 PM

No offense, just wanted to know reason using Injected..? :)

Vincent
Vincent Apr 29, 2015 06:57 AM

To Valdis

Is it used for querying the pagedata from startpage reference?

Feb 14, 2017 06:01 PM

Thanks Eric, nice & simple => a perfect solution.

Eric
Eric Feb 15, 2017 09:02 AM

Happy you like it :) 

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024