简体   繁体   English

.Net Core Web API无法添加到控制器操作的路由

[英].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. 我有一个全新的.NET Core Web API应用程序,我正在努力。 I've generated a new controller, which added the Route attribute to the Controller, so the HTTP Method is the parameter. 我已经生成了一个新的控制器,它将Route属性添加到Controller,因此HTTP方法是参数。 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. 我想更改它,以便ActionName是路由的一部分,但是在动作上放置一个Route属性似乎不起作用。 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? 我刚拿到404.我做错了什么?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 向ASP.NET Web API Controller添加显式操作路由 - Adding an explicit action route to ASP.NET Web API Controller 如何在.NET Core Web API中路由控制器类? - How to route controller class in .net core web api? 输入未映射的动作路由时执行ASP.NET Web API控制器功能 - ASP.NET Web API Controller Function Executes when entering not mapped action route 在ASP.NET Core Web API中使用依赖项注入时未调用Controller Action方法 - Controller Action method not called while using dependency injection in asp.net core web api 无法在ASP.Net Core Web API中启用CORS - Can't enable CORS in ASP.Net Core web api 无法创建ASP Net Core 2.0 Web API - Can't create asp net core 2.0 web api 无法将重写规则添加到web.config .net核心 - Can't add rewrite rules to web.config .net core 无法同时路由到“ {controller} / {action} / {id}”和“ {controller} / {id}” - Can't route to both “{controller}/{action}/{id}” and “{controller}/{id}” ASP.NET Web API - 我怎样才能有一个匹配的 Z594C103F2C6E04C3D8AB059F031 路由? - ASP.NET Web API - how can I have one controller that will match ANY route? 需要在asp.net核心web api controller方法中添加Json序列化设置 - need to add the Json serialization settings in asp.net core web api controller method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM