我是ASP核心webAPI的新手。希望你们能帮帮我。
我已经成功使用HttpGet {id}-邮递员一直在提供正确的信息。但是,我希望能够在URL中使用“ partNumber”。
以下是工作ID的获取方法:
'''
[HttpGet("{id}")]
public async Task<ActionResult<Model>> GetModel(int id)
{
return await _context.Models.FindAsync(id);
}
'''
这是我要搜索的实际参数:
```
[HttpGet("{PartNumber}")]
public async Task<ActionResult<Model>> GetModel(int partNumber)
{
return await _context.Models.FindAsync(partNumber);
}
```
当我应该看到返回的数据时,邮递员返回的404在后者中找不到。
任何帮助将不胜感激。谢谢
404表示请求网址不正确。
这是我的整个后端代码,我的控制器包含[Route("api/[controller]")]
属性:
[Route("api/[controller]")]
[ApiController]
public class TestsController : ControllerBase
{
[HttpGet("{partNumber}")]
public async Task<ActionResult<Model>> GetModel(int partNumber)
{
}
}
请求网址应为:https://localhost:44371/api/tests/YourPortNumber
(https:// localhost:44371 / api / tests / 1)。
结果:
对于邮递员中的NoContent返回,请设置断点并检查数据库是否包含具有特定partNumber的记录:
[HttpGet("{partNumber}")]
public async Task<ActionResult<Model>> GetModel(int partNumber)
{
//set breakpoint here...
var data = await _context.Models.FindAsync(partNumber);
return await _context.Models.FindAsync(partNumber);
}
结果:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句