【问题标题】:Multiple Http Gets in one controller在一个控制器中获取多个 Http
【发布时间】:2021-12-15 20:33:05
【问题描述】:

我的 Web API 应用程序正在运行,我可以使用主键进行 GET,但我需要能够使用其他字段(例如 Widgetname)进行 GET,并且我知道我需要指定 '[Route("api/[controller ]/[action]")]' 才能正常工作。 “GetByID”操作有效,但“GetByName”操作无效。我将我正在做的实际工作的名称更改为“Widget”,所以我可能没有正确重命名所有内容。代码可以编译,但是当我尝试对“GetByName”进行 API 调用时,出现 404 错误。这是我的代码:

namespace WidgetAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WidgetStuffController : ControllerBase
    {
        private readonly WidgetDbContext _context;

        public WidgetStuffController(WidgetDbContext context)
        {
            _context = context;
        }

        // GET: api/WidgetStuff
        [HttpGet]
        public async Task<ActionResult<IEnumerable<WidgetStuff>>> GetWidgetStuff()
        {
            return await _context.StuffHosts.ToListAsync();
        }

        // GET: api/WidgetStuff/GetByID
        [HttpGet("{ID}"), ActionName("GetByID")]
        public async Task<ActionResult<WidgetStuff>> GetByUUID(string ID)
        {
            var widgetStuff = await _context.StuffHosts.FindAsync(ID);

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

        // GET: api/WidgetStuff/GetByName
        [HttpGet("{Name}"), ActionName("GetByName")]
        public async Task<ActionResult<WidgetStuff>> GetByName(string Name)
        {
            var widgetStuff = await _context.StuffHosts.FindAsync(Name);

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

    }
}

如果您需要查看我的 DBContext 或模型,请告诉我。

【问题讨论】:

  • 请发布您用于 GetByName 和 GetById 的完整网址
  • 网址:https://localhost:&lt;port&gt;/api/WidgetStuff/GetByID/&lt;ID&gt;https://localhost:&lt;port&gt;/api/WidgetStuff/GetByName/&lt;Name&gt;
  • 尝试调试 - 此代码应该可以工作 if (widgetStuff == null) { return NotFound(); } 您可能在数据库中找不到任何密钥,因此返回 404。

标签: c# asp.net-core-mvc


【解决方案1】:

您的代码可能正在访问控制器,然后主动返回 404。

    [HttpGet("{Name}"), ActionName("GetByName")]
    public async Task<ActionResult<WidgetStuff>> GetByName(string Name)
    {
        var widgetStuff = await _context.StuffHosts.FindAsync(Name);

        if (widgetStuff == null)
        {
            return NotFound(); <---- THIS RETURNS A 404
        }
        return widgetStuff;
    }

问题是FindAsync,它适用于主键(可能是您表中的 Id)。

尝试替换这一行:

var widgetStuff = await _context.StuffHosts.FindAsync(Name);

与:

var widgetStuff = await _context.StuffHosts.SingleOrDefaultAsync(a => a.Name == Name);

【讨论】:

  • SBFrancies - 就是这样。我认为这与我在主键以外的字段上搜索这一事实有关。如果我搜索返回多个非关键字段怎么办?我想我知道。我会试一试的。
【解决方案2】:

我认为您的代码没有达到终点

这就是我在 dot.net core 中进行 api 调用的方式。

[HttpGet]
[Route("GetByName/{Name}")]
public async Task<ActionResult<WidgetStuff>> GetByName(string Name)
        {
            var widgetStuff = await _context.StuffHosts.FindAsync(Name);

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

终点

localhost:5000/api/yourcontroller/GetByName/nospaces_unless_escaped

设置一个断点,看看你的代码是否抛出错误。

[ProducesResponseType(typeof(ReviewView), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ReviewView), StatusCodes.Status500OK)] [ProducesResponseType(typeof(ReviewView), StatusCodes.Status404OK)]

【讨论】:

  • 我对带有参数的 HttpGet 进行了这些更改,GetByID 仍然有效,但 GetByName 无效。这可能是因为我在 DBContext 中定义的键吗?
  • 我得到了 404,所以我认为没有。这是回复{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.4","title":"Not Found","status":404,"traceId":"00-8f15c74e9f10db4abc49db139632b9a0-b60d8fccb39e434a-00"}
  • 如果您没有传递 id,请发帖而不是获取
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-02
  • 2014-03-27
  • 2019-06-14
  • 2015-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多