Tag Archives: routes

Dynamic Robots.txt with Web Api 2

Hi everyone,

For a project I’m currently working on I needed a dynamic robots.txt. Because our test environment is public facing we want to keep it from being indexed by Google etc. It took a bit of Googling to find a solution that worked, but in the end it was actually pretty simple.

Here’s the action in one of the API Controllers:

    public class UtilitiesController : CustomBaseApiController
    {
        [Route("Robots.txt")]
        [HttpGet]
        public HttpResponseMessage GetRobotsFile()
        {
            var resp = new HttpResponseMessage(HttpStatusCode.OK);
            var stringBuilder = new StringBuilder();
 
            if (Helpers.IsProduction())
            {
                // Allow bots in production
                stringBuilder.AppendLine("user-agent: *");
                stringBuilder.AppendLine("disallow: ");
            }
            else
            {
                // Don't allow bots in non-production environments
                stringBuilder.AppendLine("user-agent: *");
                stringBuilder.AppendLine("disallow: *");
            }
 
            resp.Content = new StringContent(stringBuilder.ToString());
 
            return resp;
        }
    }

Also need to add the following to your web.config so that the robots.txt file can processed by the routing handler. Without this IIS will attempt to serve it as a static file and will return a 404 when it’s not found:

<system.webServer>
    <modules>
    <!-- ... -->
        <handlers>
        <add name="RobotsTxt" path="robots.txt" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
        <!-- Other handlers -->
    </modules>
    <!-- ... -->
</system.webServer>

In production you’ll end up with the following:

user-agent: *
disallow:

And any other environments:

user-agent: *
disallow: *

Thanks to these answers on stackoverflow for the info:
https://stackoverflow.com/a/52270877/522859
https://stackoverflow.com/a/17037935/522859

Route Parameters – AngularJS

Hey everyone,

This is just a quick post on what I did to get my routes working properly with AngularJS. My routes looked like this:

//Configure routes
app.config(function ($routeProvider) {
    $routeProvider
		//...
        .when('/Businesses/Details/:id', {
            templateUrl: '/Businesses/Details'
        })
        //...
	.otherwise({
	    redirectTo: '/'
	});
});

And then to access the ID parameter I used $routeParams:

 
/* Used to display the lesson planner for customers */
app.controller("CustomerPlannerController", function ($scope, $routeParams /*, ... */) {
     console.log($routeParams);
     console.log($routeParams.id);
}

Using that setup, a route to /Businesses/Details/:id will display the following output in the console:

//Route to: /Businesses/Details/:id
Object {id: "1"} 
1

Note that you can call ID whatever you want, just make sure you change your route to match. Hopefully that’ll be enough to get you going, but if you have any issues check out these links:
http://stackoverflow.com/a/11064503/522859
http://docs.angularjs.org/api/ngRoute.$routeParams

Devise Overriding Controller Route – Ruby on Rails

I ran into a bit of a problem today after implementing a search form that appeared on every page. When the login page was loaded I received the following error:

No route matches {:controller=>”devise/products”, :method=>:get, :action=>”search”}

The weird thing about this is that the route my search form uses does not mention devise in any way:

<%= form_tag :controller => 'products', :action => 'search', :method => :get do %>

As you have probably already guessed, I’m using devise to handle my authentication. Unfortunately this seems to be overriding some of my routes whenever it co-exists with another form. Luckily there exists a very simple, although hard to find solution – simply add a forward slash before the controller definition:

<%= form_tag :controller => '/products', :action => 'search', :method => :get do %>

Here’s a quick link to the StackOverflow post which gave the answer, hopefully a few more links to it will make it a little easier to find for anyone else out there doing a search!

Error 324 (net::ERR_EMPTY_RESPONSE) – Ruby on Rails

I managed to break my app again while playing around with the routes and a few redirects:

No data received
Unable to load the webpage because the server sent no data.
Here are some suggestions:
Reload this webpage later.
Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data.

 

This wasn’t one I had encountered before and unfortunately Google couldn’t give me anything to cheat off. Luckily, a quick review of the server log revealed the following message just before it closed the connection:

[2011-12-03 21:09:05] ERROR URI::InvalidURIError: the scheme http does not accept registry part: 192.168.1.3:3000locations (or bad hostname?)

 

From this error message it became clear that I needed to place a forward slash before my redirect path:

  1. #Redirect to edit location path      
  2. redirect_to '/locations/edit'

 

Hopefully this’ll be able to help someone else out there, Good Luck!

Routing Error – No Route Matches [Post] “orders/new”

I ran into a fairly common routing error this morning, thankfully these are fairly easy to fix – usually!

Routing Error
No route matches [POST] “/orders/new”

Simply add the following the route to your routes.rb file:

\site\config\routes.rb

#Orders
controller :orders do
post ‘orders/new’ => ‘orders#new’
end

You may also have to restart WEBrick for this new route to take affect. Good Luck!