Click here to Skip to main content
Click here to Skip to main content

Attribute Routing

, 3 Mar 2014
Rate this:
Please Sign up or sign in to vote.
One of the new things that seems to have past me by when working with ASP MVC / Web API is Attribute Routing. This is not intended to be a big blog post, but will show you one reason why Attribute Routing is useful.   Imagine we have this Web API controller, which are both […]

One of the new things that seems to have past me by when working with ASP MVC / Web API is Attribute Routing. This is not intended to be a big blog post, but will show you one reason why Attribute Routing is useful.

Imagine we have this Web API controller, which are both valid GET urls

image

Which one of these would be chosen when hitting the ValuesController url : http://localhost:1744/api/values

In fact if you look at this screen shot you will see it is an issue for Web API routing

image

Mmmm maybe there is something we can do about this.In fact there is we just need to grab the fairly new Nu-Get package for AttributeRouting.WebApi

Install-Package AttributeRouting.WebApi

After you have run this you will get a automatically created VB|C# file that sets up your WebApi project to allow AttributeRouting. It looks like this

image

Now that we have the infrastructure in place we can start to use the new RoutingAttribute types.

One of the really useful one is HttpRouteAttribute, which I can use like this:

image

Which now allows me to do go to this URL with ease : http://localhost:1744/api/values/last

Which I personally find much easier than defining loads of custom routings manually. OK they are still manual, but I do find attributes easier to use.

image

I have just shown one thing that the new AttributeRouting NuGet package allows. For more information you can see these few posts which are more in depth.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sacha Barber
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)
 
- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence
 
Both of these at Sussex University UK.
 
Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 3 Mar 2014
Article Copyright 2014 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid