Using MVC Attribute Routing

Now that Visual Studio 2013 has been officially released, one of the new features that I have been looking forward to use, is the new attribute based routing

My MotorDB project was created using Visual Studio 2012. So after installing Visual Studio 2013, I opened up the project in 2013 and compile. Everything still compiles.

I initially struggled to get the project updated to use MVC 5, I eventually found a tutorial on the web site: How to Upgrade an ASP.NET MVC 4 and Web API Project to ASP.NET MVC 5 and Web API 2

Problems with Git

So I wanted to roll back all the changed I had made, issuing the following commands

git reset
git checkout
git clean -fdx

But it did not roll anything back. I know I have probably made a mistake. That will teach me to work in the master branch. As I am also learning git, the easiest option was to delete the project solution folder, and clone again from github.

For all my work, I create new projects off folder c:\MyProjects. When I first cloned the project from github I was in folder c:\MyProjects\MotorDB, but when it started the clone it created a new sub folder. Deleted sub-folder and started again.

This time, lesson learnt. Started a new branch in my local git repository.

Attribute Routing

After cloning the project, and following the tutorial from site, I was ready to start again.
First was to open up WebApiConfig, and remove the following route definition from the Register method

    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }

I did this because I wanted to make sure that whatever I did, I did not want the default route to work.

After deleting the above statement, then add in the following line


to WebApiConfig Register method

As this project only has one WebApi controller, the PolicyController is what going to modify

All that we needed to do was add one attribute to the class


I just added an “s” so that the when I called the API, I could ensure that it was testing the route.

Project still compiles, but then run the application, no data is displayed.

I took this as a good sign as it meant that the old default route was no longer working. With the site running, start fiddler and started testing for uri’s


Getting 404 error

Try http://localhost:50634/api/policys/

Result!!! Getting my static data returned.

All that I needed to do was update the typescript file with the url location

module MotorDB {
    export class DataService {
        public GetAllPolicys() {
            return $.ajax({
                url: 'api/policys',
                type: 'GET'

Voila. Everything works again, but now using attribute based routing.

The code will be posted to github shortly as soon as I can figure out how to commit my changes back to my master branch.


My musing about anything and everything

Tagged with: ,
Posted in MotorDB
2 comments on “Using MVC Attribute Routing
  1. […] Using MVC Attribute Routing ( […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 13 other subscribers
%d bloggers like this: