Blog

Posts written in December 2014

Dynamic Robots.txt file for ASP.NET MVC Sites

Posted in: ASP.NET

Changing the contents of a robots.txt file when a site is moved from staging to a live environment is quite a manual and somewhat cumbersome process. I sometimes have the fear of forgetting to replace the "Disallow: /" line with correct indexable entries required for the website.

To give me one less thing to remember during my pre-live deployments, all my current and upcoming ASP.NET MVC sites will use a dynamic robots.txt file containing different entries depending on whether a site is in stage or live. In order to do this, we need to let the ASP.NET MVC application serve up the robots.txt file. This can be done by the following:

  • Create a new controller called "SEOController"
  • Add a new FileContentResult action called "Robots".
  • Add a new route.
  • Modify web.config.

SEOController

I have created a new controller that renders our "Robots" action as a plain text file. As you can see, my controller is not a type of "ActionResult" but a "FileContentResult". The great thing about "FileContentResult" is that it allows us to return bytes from the controller.

In this example, I am converting bytes from a string using Encoding.UTF8.GetBytes() method. Ideal for what we need to generate a robots.txt file.

[Route("robots.txt")]
[Route("Robots.txt")]
public FileContentResult Robots()
{
    StringBuilder robotsEntries = new StringBuilder();
    robotsEntries.AppendLine("User-agent: *");

    //If the website is in debug mode, then set the robots.txt file to not index the site.
    if (System.Web.HttpContext.Current.IsDebuggingEnabled)
    {
        robotsEntries.AppendLine("Disallow: /");
    }
    else
    {
        robotsEntries.AppendLine("Disallow: /Error");
        robotsEntries.AppendLine("Disallow: /resources");
        robotsEntries.AppendLine("Sitemap: http://www.surinderbhomra.com/sitemap.xml");
    }

    return File(Encoding.UTF8.GetBytes(robotsEntries.ToString()), "text/plain");
}

RouteConfig

Since I add my routing at controller level, I add the "MapMvcAttributeRoutes" method to the RouteConfig.cs file. But if you prefer to add your routes directly here, then this method can be removed.

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes(); //Add this line!

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Web.config

Add a "runAllManagedModulesForAllRequests" attribute to the modules tag in the web.config file to allow our robot.txt text file to be rendered by ASP.NET.

<system.webserver>
  <modules runallmanagedmodulesforallrequests="true"></modules>
</system.webserver>

What's Currently Wrong With Star Trek Franchise?

Posted in: Random Thoughts

NOTE: I write this post as a person who just appreciates the Star Trek movie franchise. Not as a massive fan.

Star Trek has always had a special place in my heart for as long as I can remember. The moment I sat down with my Dad and watched Star Trek III: Search for Spock with fresh new eyes, I was instantly grabbed by the action, ships, characters and vastness of space. It didn't take me long to understand the basic premise of the show even if I didn't completely understand the plot points.

I've dabbled in and out of the franchise over the years. But ever since the reboot in 2009, I have gained a renewed interest and started to look back at the classic movies with much fondness and appreciation. I truly admire how clever and gripping they were. Of course, there are some bad eggs when it came to the sequels (I'm talking about you Star Trek V!), nevertheless, they always managed to have memorable scenes.

Thoughts On The New Trek

I thought the reboot was off to a great start (no matter what the naysayers say), JJ Abrams managed to give Star Trek the kick in the butt it (quite frankly!) needed and looked forward to many more adventures with the characters I have grown to love. Star Trek became exciting again!

Four years later, we were given Star Trek Into Darkness and I remember leaving the cinema quite satisfied. But after I let the film digest within my subconscious, I started to pick holes in the storyline and came to the conclusion the highly anticipated sequel didn't actually offer anything new. It felt like a remake of Star Trek II: The Wrath of Kahn pure and simple, a film that I will always hold dear to my heart. A film that contains themes of revenge, coming of age, friendship and sacrifice.

So what's wrong with the current incarnation of Star Trek?

1) Lack of Continuity Between Films

One of the finest moment of the original films for me was the continuity between films that had an underlying theme that linked them together. Of course, I am talking about movies II - IV. It was quite satisfying seeing the characters develop and grow based on the decisions made from the previous films.

You might be thinking: What am I complaining about? The current reboot hasn't had a chance to grow yet?

Valid point. However, I believe this is something the writers could have done already. Planted the seed to grow in future adventures.

2) The Length of Time Between Sequels

The main problem with the Star Trek franchise is the length of time between installments. The films need to be out more frequently. Currently, the length of time between films is 4 years. This is too long, especially if a film hasn't been as strong in the story department, nor met the expectations of fans. I think it's safe to say Into Darkness was not embraced as positively when compared to the 2009 film.

What impact does this have? A big one. The momentum just disappears and the franchise has the added pressure of having to re-establish itself again to the public.

As it stands, the cast, designs and production are all delivered to a very high standard. But if the likes of Paramount spend too long about the story they wish to tell, they really need to get better script writers and directors with a coherent vision.

Come on Paramount! Pull your finger out!

3) Bring Writers In Who Aren't Fans of The Trek-lore

Some of the best Trek have come from people who aren't avid fans of the world of Star Trek. They have the ability to take a step back in order to create a unique and interesting vision. They take important facets of the characters and then weave Star Trek into the story.

I personally would love to see Nicholas Meyer have the opportunity to write the next installment. Now that would be something to look forward to!

4) Retreading The Same Ground

For those who have watched Star Trek in film or TV form and made a comparison with the current Trek movies, you would get a sense of deja-vu. As I've briefly stated above, there doesn't seem to be anything new being brought to the screens that we haven't already seen before, which is a little disappointing for the Trek veterans.

Into Darkness could have been the film to show something new and original. Retreading old ground in a universe purely built to tell new stories was never going to go down well.

5) No More Spock Prime

I loved seeing Leonard Nimoy reprise his role as Spock Prime in the 2009 reboot. It was pure genius and managed to make a connection with all previous films so that they still remained relevant. This was probably a ploy to get buy in from the proper Star Trek fans. I for one appreciated the sentiment.

However, it was unnecessary bringing him back for a brief appearance in Into Darkness and dare I say...a little cringe worthy.

Final Thoughts

There is so much to explore in the Star Trek universe and the formula to get Star Trek right is not as complex as it may seem. The script writers need to take a good hard look at what made the original films so successful (without plagiarising!) and make us a Trek film where no one has gone before...

;