4

I've got a brand new .NET Core Web API application that I'm working on. I've generated a new controller, which added the Route attribute to the Controller, so the HTTP Method is the parameter. I want to change it so the ActionName is part of the route, but putting a Route attribute on the action doesn't seem to be working. So my controller is set up like this currently:

[Produces("application/json")]
[Route("api/Spells")]
public class SpellsController : Controller
{
    private readonly Spellbook3APIContext _context;

    public SpellsController(Spellbook3APIContext context)
    {
        _context = context;
    }

    // GET: api/Spells
    [HttpGet]
    public IEnumerable<Spell> GetSpells()
    {
        return _context.Spells;
    }
}

I want to do it this way:

[Produces("application/json")]
public class SpellsController : Controller
{
    private readonly Spellbook3APIContext _context;

    public SpellsController(Spellbook3APIContext context)
    {
        _context = context;
    }

    // GET: api/Spells
    [HttpGet]
    [Route("api/Spells/GetSpells")]
    public IEnumerable<Spell> GetSpells()
    {
        return _context.Spells;
    }
}

But when I put that, it doesn't work. I just get a 404. What am I doing wrong?

1 Answer 1

4
 [HttpGet("GetSpells")]
 public IEnumerable<Spell> GetSpells()
 {
     return _context.Spells;
 }
Sign up to request clarification or add additional context in comments.

5 Comments

That's still 404ing for me
updated more code, does this one work? ...added the route at the class level
Sadly no. :( Also you're missing a square bracket on the right of [Route("/GetSpells")] but that wasn't my issue
try adding the route to the httpget parameter? and make sure the class has the route attribute
Aha! That did it! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.