What the...?

Something bad happened

We're not sure what, but we have a few guesses.

Problem: Object reference not set to an instance of an object.
Ticket: 7864830
Server: Web03
Applying Attribute Routing in ASP.NET Web API REST Methods - CodeProject Click here to Skip to main content
11,715,022 members (64,929 online)
Click here to Skip to main content

Applying Attribute Routing in ASP.NET Web API REST Methods

, 20 Jan 2014 CPOL 9.3K 5
Rate this:
Please Sign up or sign in to vote.
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
 type HandheldServer.Controllers.DuckbilledPlatypiController System.Collections.Generic.IEnumerable`1
[HandheldServer.Models.DuckbilledPlatypus] GetAllDuckbilledPlatypi() on type
 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

B. Clay Shannon
Founder "Across Time & Space"
United States United States
Ideaman and Coder at Across Time & Space, creator of "Mark Twain Central" at http://twaincentral.azurewebsites.net/

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction: http://www.lulu.com/spotlight/blackbirdcraven

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
arshu191910-Aug-15 23:42
memberarshu191910-Aug-15 23:42 
GeneralMy vote of 5 Pin
Member 947097426-Dec-14 10:29
memberMember 947097426-Dec-14 10:29 
QuestionWow Pin
Christian Graus20-Jan-14 15:33
protectorChristian Graus20-Jan-14 15:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150901.1 | Last Updated 20 Jan 2014
Article Copyright 2014 by B. Clay Shannon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid