Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Applying Attribute Routing in ASP.NET Web API REST Methods

0.00/5 (No votes)
20 Jan 2014 1  
How to apply Attribute Routing to Web API Controllers

Where's My Serrated Knife?

At the risk of seeming strident or going on out on a limburger, I hereby assert that Attribute Routing (caps deliberate) is the best thing since sliced bagels.

The ASP.NET Web API REST extravaganza (and its brethren) use, where possible, convention over configuration. That is to say, you can name a method in your Controller Get (something), such as GetAJob() or GetReal(), and HTTP GET methods will invoke it.

If you have multiple Get methods, as long as they have a unique signature, all is well.

But what if you have two parameterless Get methods, such as one to get the count of items (returning an int) and another to get all the items (returning a collection of a particular type)? 

Cue the Bugles (not the Bagels, the Bugles)!

Never fear - Attribute Routing is here!

Here's an example of two Controller "GET" methods that have no arguments:

        public int GetCountOfDuckbilledPlatypiRecords()
        {
            return _DuckbilledPlatypusRepository.GetCount();
        }

        public IEnumerable<duckbilledplatypus> GetAllDuckbilledPlatypi()
        {
            return _DuckbilledPlatypusRepository.GetAll();
        }

If I run the Web API app and enter in the browser: "http://localhost:28642/api/DuckbilledPlatypi" I confuse the Charles Dickens out of the router, which can only say:

"Multiple actions were found that match the request: Int32 GetCountOfDuckbilledPlatypiRecords() on<br /> type HandheldServer.Controllers.DuckbilledPlatypiController System.Collections.Generic.IEnumerable`1<br />[HandheldServer.Models.DuckbilledPlatypus] GetAllDuckbilledPlatypi() on type<br /> HandheldServer.Controllers.DuckbilledPlatypiController" 

But if I add Attribute Routing, like so:

        [Route("api/DuckbilledPlatypi/Count")]
        public int GetCountOfDuckbilledPlatypiRecords()
        {
            return _DuckbilledPlatypusRepository.GetCount();
        }

        [Route("api/DuckbilledPlatypi/GetAll")]
        public IEnumerable<duckbilledplatypus> GetAllDuckbilledPlatypi()
        {
            return _DuckbilledPlatypusRepository.GetAll();
        }

...and then enter "http://localhost:28642/api/DuckbilledPlatypi/Count" I get 42 (or whatever - depends on how many Platypi I have, of course.)

And If I enter http://localhost:28642/api/DuckbilledPlatypi/GetAll, I get, as hoped, the entire collection back.

The Custard and Mustard Cavalry

So: when you need diverse "queries" in your Controller, it's Attribute Routing to the rescue!

Note: The new update (ASP.NET MVC 5.1) improves attribute routing, even. See the official release notes

Speaking of rescue, if you like this tip, be sure to generously tip the next member of a waitstaff that serves you.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here