Asp.net Core Web API中的属性路由

时间:2019-05-15 02:10:26

标签: c# asp.net-core asp.net-core-webapi asp.net-core-routing

我尝试将asp.net核心Web api路由属性启动为默认路由,但是当我使用参数访问路由时,我无法得到任何响应

[Route("api/[controller]")]
[ApiController]
public class WarehousesController : ControllerBase
{
    private readonly ApplicationDbContext _context;

    public WarehousesController(ApplicationDbContext context)
    {
        _context = context;
    }

    //http://localhost:2394/api/Warehouses/Project/1 (Not working)
    [HttpGet("Project/{projectId}")]
    public async Task<IActionResult> GetWarehouseByProjectId([FromRoute] int projectId)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var warehouse = await _context.warehouses.Include(x => x.Projects).Where(y => y.Projects.FirstOrDefault().ProjectId == projectId).ToListAsync();

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

        return Ok(warehouse);
    }

}

0 个答案:

没有答案