1

I've got an ASP.NET Web API project that I'm working on. I've got an APIController called SpellbookController that has basic CRUD operations to an EntityFramework repository.

However, when I try to add a method that takes a parameter that's not an id, I can't seem to get it to route correctly.

Here's what it looks like:

// GET: api/Spellbooks/user@email.com
[ResponseType(typeof(List<Spellbook>))]
[Route("api/Spellbooks/{username}")]
public IHttpActionResult GetSpellbook(string username)
{
    List<Spellbook> spellbooks = db.Spellbooks.Where(x=>x.Username == username).ToList();
    if (spellbooks == null)
    {
        return NotFound();
    }

    return Ok(spellbooks);
}

So I'm trying to hit http://localhost:xxxx/api/Spellbooks/emailaddress, but I get a 404 every time.

It's definitely an APIController, and I have config.MapHttpAttributeRoutes(); turned on.

What am I missing?

AGB
  • 2,230
  • 1
  • 14
  • 21
Alex Kibler
  • 4,674
  • 9
  • 44
  • 74

2 Answers2

0

Where is your username parameter?

Your call should looks like this-

 http://localhost:xxxx/api/Spellbooks/emailaddress/David

Update

Try to declare the parameter as string {username:string}

Update 2

The problem is with your '.' as you can see in this link. You can send the email without the point in the parameter and then replace ite back to you regular mode or you can use all the good advice that the link provide.

Community
  • 1
  • 1
The scion
  • 1,001
  • 9
  • 19
  • oh sorry, `emailaddress` is the parameter I'm talking about, because their username is their email address. – Alex Kibler May 14 '16 at 18:09
  • So I guess I'm calling it http://localhost:xxxx/api/Spellbooks/test@test.com. does that help? – Alex Kibler May 14 '16 at 18:09
  • Try to do it with simple string, and see what happen. Maybe there is problem with the email as string. – The scion May 14 '16 at 18:11
  • That was it! Is there any way to send an email address as a string? – Alex Kibler May 14 '16 at 18:16
  • @AlexKibler, I just added update, does the first update solve your problem?so I will delete the update 2 section... – The scion May 14 '16 at 19:00
  • I actually just decided to use a userId instead of the email, but your second update would work perfectly, according to that link. I marked this accepted because it was your advice that led me to getting the right answer – Alex Kibler May 14 '16 at 19:03
0

If you step through it, do you get a value when looking at username? Or is it always null?

If your route is not working, a simple way to debug the route is to make a request to http://localhost:xxxx/api/Spellbooks/?emailaddress=thisemailaddress and see if you can get a response. In fact, from a REST standard, it can be argues that it's a cleaner route, since you will be returning a collection of elements, rather than a single object.

napo
  • 869
  • 9
  • 19
  • It wasn't capable of being stepped through. it was ASP.NET that was throwing the 404. However, I ended up just switching to a UserId instead of a username, and that's working just fine. – Alex Kibler May 14 '16 at 19:09
  • So, basically, it was the route that wasn't getting hit? – napo May 14 '16 at 19:10