Alex Kibler
Alex Kibler

Reputation: 4954

.Net Core Web API can't add route to controller action

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?

Upvotes: 4

Views: 1104

Answers (1)

Bryan Stump
Bryan Stump

Reputation: 1439

 [HttpGet("GetSpells")]
 public IEnumerable<Spell> GetSpells()
 {
     return _context.Spells;
 }

Upvotes: 4

Related Questions