首页 > 解决方案 > 未使用属性调用 HttpGet 方法

问题描述

GetItems方法工作正常GetItem,但属性“id”不是。当我发送 GET 请求时,我得到了所有返回的项目,这意味着GetItems()被调用。我错过了什么?

// GET request: https://localhost:xxxxx/api/Items
// Working
[HttpGet]
public async Task<ActionResult<IEnumerable<Item>>> GetItems()
{
    return await _context.items.ToListAsync();
}

// GET request: https://localhost:xxxxx/api/Items/2
// Not running
[HttpGet("{id}")]
public async Task<ActionResult<Item>> GetItem(long id)
{
   var item = await _context.items.FindAsync(id);

    if (item == null)
    {
        return NotFound();
    }

    return item;
}

我想过滤带有名称的项目,但这与上面的 GetItem(long id) 有相同的问题,所有项目都被返回。

GET request: https://localhost:xxxxx/api/Items?name=aaa 
    
// Not running
[HttpGet("{name}")]
public async Task<ActionResult<IEnumerable<Item>>> GetItems(string name)
{
    var item = await _context.items.Where(x => x.Name == name).ToListAsync();

    if (item == null)
    {
        return NotFound();
    }

    return item;
}

[HttpPost]
   public async Task<ActionResult<TodoItem>> PostTodoItem(TodoItem todoItem)
        {
            _context.TodoItems.Add(todoItem);
            await _context.SaveChangesAsync();
            return CreatedAtAction(nameof(GetTodoItemById), new { id = todoItem.Id }, todoItem);
        }

响应示例:

{
    "id": 1,
    "name": "aaa",
    "isRegistered": true
}
{
    "id": 2,
    "name": "aaa",
    "isRegistered": false
}
{
    "id": 3,
    "name": "bbb",
    "isRegistered": false
}

在 Startup.cs 中配置方法:

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

        }

标签: c#asp.net-core

解决方案


您可以像这样添加Action到您的Route[Route("api/[controller]/[action]")],下面是一个工作演示:

[Route("api/[controller]/[action]")]
[ApiController]
public class ItemsController : ControllerBase
{
   //...

    [HttpGet]
    public async Task<ActionResult<IEnumerable<Item>>> GetItems()
    {
        return await _context.Items.ToListAsync();
    }


    [HttpGet]
    public async Task<ActionResult<Item>> GetItemById(int id)
    {
       
        var item = await _context.Items.FindAsync(id);

        if (item == null)
        {
            return NotFound();
        }

        return item;
    }
    [HttpGet]
    public async Task<ActionResult<IEnumerable<Item>>> GetItemByName(string name)
    {
        var item = await _context.Items.Where(x => x.Name == name).ToListAsync();

        if (item == null)
        {
            return NotFound();
        }

        return item;
    }
}

获取请求:

1:https://localhost:xxxxx/api/Items/getitems

结果: 在此处输入图像描述

2:https://localhost:xxxxx/api/Items/GetItemById?id=xx

结果: 在此处输入图像描述

3:https://localhost:xxxxx/api/Items/GetItemByName?name=xx

结果: 在此处输入图像描述

发帖请求:

https://localhost:xxxxx/api/Items/todoitem

结果:

在此处输入图像描述


推荐阅读