asp.net - .Net Core Web API 无法将路由添加到 Controller 操作

标签 asp.net asp.net-web-api asp.net-core-webapi

我正在开发一个全新的 .NET Core Web API 应用程序。我已经生成了一个新的 Controller ,它给 Controller 添加了路由属性,所以 HTTP 方法是参数。我想更改它,以便 ActionName 是路由的一部分,但是在操作上放置 Route 属性似乎不起作用。所以我的 Controller 目前是这样设置的:

[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;
    }
}

我想这样做:
[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;
    }
}

但是当我把它放在那里时,它不起作用。我刚收到 404。我做错了什么?

最佳答案

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

关于asp.net - .Net Core Web API 无法将路由添加到 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43767386/

相关文章:

c# - HttpResponseMessage 你处理了吗?

asp.net - asp.net-vnext 中的旧程序集引用错误

c# - Dotnet Core API - 获取 Controller 方法的 URL

asp.net-web-api - 仅对特定操作使用驼峰式序列化

c# - 添加对 ASP.NET Web API 服务的服务引用

c# - ASP.NET Core Web API 自定义路由不起作用

c# - Chrome PDF 查看器 "Save To Disk"功能 - 是否可以控制保存的文件扩展名?

c# - ASP.NET 错误 System.ComponentModel.Win32Exception : 'The system cannot find the file specified'

javascript - ASP :Button Click Event doesn't fire

c# - 使用 sqlite 的 Entity Framework Core 中的奇怪行为