问题描述
我已经在我的情况控制器中创建了一个方法,以JSON格式获取情况列表。该方法称为GetSituations。
namespace Amaz.API.Controllers
{
[ServiceFilter(typeof(LogUserActivity))]
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class SituationController : ControllerBase
{
private readonly IVVRepository _repo;
private readonly IMapper _mapper;
public SituationController(IVVRepository repo,IMapper mapper)
{
_mapper = mapper;
_repo = repo;
}
[HttpGet("{UserId}")]
public async Task<IActionResult> GetSituations (int UserId)
{
var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
var situationreturn = await _repo.GetSituations(UserId);
if (situationreturn == null)
return NotFound();
return Ok(situationreturn);
}
[HttpGet("{id}")]
public async Task<IActionResult> GetSituation (int id)
{
// if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
// return Unauthorized();
var situationreturn = await _repo.GetSituation(id);
if (situationreturn == null)
return NotFound();
return Ok(situationreturn);
}
[HttpPost]
public async Task<IActionResult> PutSituation(SituationDto SituationDto)
{
var SituationToCreate = _mapper.Map<Situation>(SituationDto);
_repo.Add(SituationToCreate);
await _repo.SaveAll();
return NoContent();
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateSituation(int id,SituationDto SituationDto)
{
var situation = await _repo.GetSituation(id);
_mapper.Map(SituationDto,situation);
return NoContent();
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteSituation (int id)
{
var SituationFromrepo = await _repo.GetSituation(id);
_repo.Delete(SituationFromrepo);
return NoContent();
}
}
}
在存储库中:
public async Task<IEnumerable<Situation>> GetSituations (int UserId)
{
var messages = _context.Messages.AsQueryable();
return (IEnumerable<Situation>)messages;
}
并在存储库的界面中:
Task<IEnumerable<Situation>> GetSituations (int UserId);
但是由于任何原因,它都无法正常工作。使用Postman测试API http://localhost:5000/api/Situation/1
时,返回仅是以下情况之一:
{
"id": 1,"description": "Limpieza","isMain": false,"userId": 1,"isApproved": true,"item1": 0,"item2": 1,"item3": 2,"item4": 3,"item5": 4,"item6": 5,"item7": 6,"item8": 7,"item9": 8,"item10": 9,"item11": 10,"item12": 11,"item13": 12,"item14": 13,"item15": 14,"item16": 15,"item17": 16,"item18": 17,"item19": 18,"item20": 19
}
但是,实际上,它应该返回存储在数据库中的所有情况。为什么它不起作用?
解决方法
不能有两个具有相同路由的HttpGet方法。当您调用该路由时,只有其中一个会被命中,而当您使用http://localhost:5000/api/Situation/1
时,它现在正在命中GetSituation端点而不是GetSituations端点。您需要向其中至少一个添加特定的路由。您可以为此使用属性路由:
[HttpGet("{UserId}")]
[Route("all")]
public async Task<IActionResult> GetSituations (int UserId)
然后尝试到达以下端点:http://localhost:5000/api/Situation/all/1
从GetSituations检索数据。
继续使用此端点:http://localhost:5000/api/Situation/1
从GetSituation检索数据。
嗯,它仍然无法正常工作。按照我定义的控制器:
[Route("api/[controller]/all")]
[HttpGet]
public async Task<IActionResult> GetSituations ()
{
var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
var situationstoreturn = await _repo.GetSituations();
if (situationstoreturn == null)
return NotFound();
return Ok(situationstoreturn);
}
接口存储库中的函数
Task<IEnumerable<Situation>> GetSituations ();
和存储库:
public async Task<IEnumerable<Situation>> GetSituations ()
{
var Situation = _context.Situations.ToListAsync();
return await Situation;
}
http://localhost:5000/api/Situation/all
的结果是:
{
"errors": {
"id": [
"The value 'all' is not valid."
]
},"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1","title": "One or more validation errors occurred.","status": 400,"traceId": "|fafb39a-479332ff01211120."
}
它本来可以工作,但是仍然失败了……我不知道这个线索……
,现在,这是最新技术。这里是控制器:
namespace Amaz.API.Controllers
{
[ServiceFilter(typeof(LogUserActivity))]
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class SituationController : ControllerBase
{
private readonly IVVRepository _repo;
private readonly IMapper _mapper;
public SituationController(IVVRepository repo,IMapper mapper)
{
_mapper = mapper;
_repo = repo;
}
//We pick UP all the situations with GET
[Route("all")]
[HttpGet]
public async Task<IActionResult> GetSituations (int UserId)
{
//Esto debería devolver el usuario
var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
var situationstoreturn = await _repo.GetSituations(UserId);
if (situationstoreturn == null)
return NotFound();
return Ok(situationstoreturn);
}
//GET only one situation
[HttpGet("{id}")]
public async Task<IActionResult> GetSituation (int id)
{
// if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
// return Unauthorized();
var situationreturn = await _repo.GetSituation(id);
if (situationreturn == null)
return NotFound();
return Ok(situationreturn);
}
//Añadimos situación POST
[HttpPost]
public async Task<IActionResult> PutSituation(SituationDto SituationDto)
{
var SituationToCreate = _mapper.Map<Situation>(SituationDto);
_repo.Add(SituationToCreate);
await _repo.SaveAll();
return NoContent();
}
//Update Situacion PUT
[HttpPut("{id}")]
public async Task<IActionResult> UpdateSituation(int id,SituationDto SituationDto)
{
var situation = await _repo.GetSituation(id);
_mapper.Map(SituationDto,situation);
return NoContent();
}
//Delete Situation DEL
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteSituation (int id)
{
var SituationFromRepo = await _repo.GetSituation(id);
_repo.Delete(SituationFromRepo);
return NoContent();
}
}
}
存储库为:
public async Task<IEnumerable<Situation>> GetSituations(int UserId)
{
var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == UserId);
var Situation = _context.Situations.ToListAsync();
return await Situation;
}
不幸的是,当我给API邮递员这个呼叫时:
http://localhost:5000/api/Situation/all/
我获得:
{
"errors": {
"id": [
"The value 'all' is not valid."
]
},"traceId": "|6e318df2-469ed81893adc4fa."
}
因此,我认为不可能在给定UserId的情况下获取数组