2010
09.03

The ASP.NET Routing module is responsible for mapping incoming browser requests to controller actions. When you create a new ASP.NET MVC application, the application is configured with some standard routes in the Global.ascx file. The route table is created during the Application Start event of the Global.ascx file.

Line 2: tells MVC to ignore any requests to .axd files and not route them to an MVC controller.

Line 3-11: adds a default route which maps to a URL which matches the format:

/Details/Index/3

The default route maps this URL to the following parameters:

controller = Details
Action = Index
ID =3

Line 8-9: sets some defaults for the controller and action, so if we type a url such as http://my-site.co.uk/ it would automatically map to the Home controller’s Index action.

Line 10: specifies that the id field is optional.

The example below shows the corresponding Index  method of the DetailsController this method is invoked by the URL /Details/Index/3 and /Details/Index

Default routes are all well and good but what if we want something not default? Well out of the box the routing in MVC is clever enough to route URLs with query string parameters on, so for example:

URL Maps to
/Search/ Index
/Search/Results?query=iphone Results

The first URL /Search/ we already understand how this maps from the previous examples but the second URL /Search/Results?query=iphoneis more interesting. This values can be read from the query string just like they could be in normal ASP.NET, whilst there is nothing wrong with this URL we can make it better. What if you wanted to use the following URLs?

URL Maps to
/Search/ Index
/Search/IPhone Results
/Search/IPhone/2 Results

We can enable these URLs by adding some additional routes into the Global.ascx file

You must add this new route above the default otherwise the default route will be mapped and you will end up in the wrong place. The route is not that different to the default one, instead of using  {controller} to specify that the URL specifies the controller we want instead the URL starts with /Search/ but instead goes to the Search Controllers Results action. Line 10: indicates that the page is optional so in the case no page is specified it is defaulted to 1 otherwise the value passed.

The Action method for this route would look like the following:

2010
09.01

You sometimes come across situations in which you need to install a ruby gem which is located on your machine. This happens frequently in work as we are behind a firewall and it can often lead to strange errors despite specifying the proxy server in the ‘gem install’ command.

For purposes of demonstration I am assuming that you want to install the rspec gem which you have downloaded from the following site: http://rubyforge.org/frs/?group_id=797. Once you have the gem on your machine, open a command prompt where you have downloaded the gem to and run this command

gem install gem-name-x.x.x --local

If you get an error saying gem-name-x.x.x needs some-other-gem then you will need to download these gem files as well and download them first. For example rspec-1.3.0 has the following dependencies.

cucumber
diff-lcs
fakefs
heckle
hoe
syntax

Assuming you have downloaded all of the dependencies and installed one by one then you will now be able to install rspec.

2010
08.12

Getting started with gist

I’ve tried lots of things for sharing and posting code and none have really worked the way I would like. I recently tried Gist

Gist is a simple way to share snippets and pastes with others. All gists are git repositories, so they are automatically versioned, forkable and usable as a git repository.

Read More >>

2010
07.28

For a project I am currently working on I have been trying to set a hidden field value using Selenium. This proved more difficult than I thought so I thought I would share how to do it. I decided to use Javascript to set the hidden field value, using Selenium you can run a script using the RunScript command.

To get access to the hidden field you have to prefix document.getElementById(...) with this.browserbot.getCurrentWindow() I was then able to successfully set a hidden field from within an automated acceptance test.

var script = "javascript{ this.browserbot.getCurrentWindow().document.getElementById('destination-id').value = '#{value}'; }"
Selenium.RunScript(script);

The reason for this is because by default the code snippet wil run in the runner’s test window, not in the window of your application.

2010
07.26

Umbraco web hosting

I am nearing the end of the development of my Umbraco site and it has come time to sort the web hosting side out. Umbraco can be from what I have read a little tricky get working due to the requirements. You will need SQL Server, ASP.NET 4.0 the hardest to satisfy though is it needs to be run in a full trust environment.

This blog post documents my experiences with pipeten and how easy or not it is to get Umbraco up and running. I started off by requesting a trial of their dedicated server windows hosting package they kindly set up a dedicated server running Windows, IIS and ASP.NET 4.

The installation process

Read More >>

2010
06.12

Previous post in series: Getting started with Umbraco

Document Types are an important element of Umbraco, a Document Type is a structure used to define how and what data to collect from a user during content editing. Document Types are often compared to database tables, it is made up of Properties, Tabs, Structure and Info.

Read More >>

2010
06.12

Umbraco is an open source CMS based on Microsoft’s ASP.NET, currently it supports up to ASP.NET 3.5 but they are working on a new version which is re-written to use Microsoft ASP.NET MVC. Umbraco is available through Web Platform Installer which takes care of installing all of the relevant dependencies on your machine.

Installing Umbraco

Read More >>

2010
06.06

On my current project I got really annoyed with SQLExpress and Entity Framework and have changed direction totally with the database and gone with a NoSQL solution using MongoDB. NoSQL is getting a lot of press at the moment, according to Wikipedia, “NoSQL is a movement promoting a loosely defined class of non-relational data stores that break a long history of relational databases. These data stores may not require fixed table schemas, usually avoid join operations and typically scale horizontally. Academics and papers typically refer to these databases as structured storage”.

There are a number of NoSQL implementations about I decided on MongoDB because of driver support for Linq queries if you use NoRm and I was able to integrate into my project with minimum disruption.

Read More >>

2010
06.04

Validating user input in ASP.NET MVC 2 has never been easier and with business logic being a core component of most web applications it has never been more important. ASP.NET MVC 2 allows you to enforce this validation logic in the model / viewmodel meaning you only have to define it once, so for example:

Read More >>

2010
04.25

I am writing a site in Ruby on Rails at the moment and to keep the views clean I have cut down duplication and complexity by moving items out into partials. There are three main ways to get data to your partial.

# Here the photo object will be available as a local variable called 'upload_photo' in the partial
< %= render :partial => "photos/upload_photo", :object => @photo %>

# Here the partial will be rendered for each photo in the collection
< %= render :partial => "photos/upload_photo", :collection => @photos %>

# Here a local variable called 'photo' will be available in the partial.
< %= render :partial => "photos/upload_photo", :locals => { :photo => @photo } %>

This works perfectly if your partials data comes from the controller that is currently serving the page content in which case you find you can just use the variable directly.

def index
  @posts = Post.all
end

In the above example you can use the @posts variable directly in your partial, what if your data for the partial comes from another page?

Read More >>