我想知道如何通过帖子传递ID和下面的路线?
// something / entry / recordcoworkers / startdate / 13
或
// something / entry / recordcoworkers / 13 / startdate
这是我的控制人
[Produces("application/json")]
[Route("api/RecordCoworkers")]
public class RecordCoworkersController : Controller
{
// POST: api/RecordCoworkers/StartDate
[HttpPost("StartDate")]
public async Task<IActionResult> PostRecordCoworkerStartDateAsync([FromRoute] int id, [FromBody] RecordCoworker recordCoworker)
{
if (!Auth.IsRequestAuthorized(_context, User.Identity.Name, "Mpid2_Entry_Write"))
{
return BadRequest();
}
var result = await RecordCoworkersContext.PostRecordCoworkerStartDateAsync(_context, id, recordCoworker);
switch (result)
{
case HttpResult.NoContent:
return Ok();
case HttpResult.NotFound:
return NotFound();
default:
return BadRequest();
}
}
}
这是axios帖子通话
axios.post('/api/RecordCoworkers/StartDate/' + id, {
id: coworkerId,
startDate: value,
}).then(function(response) {
// Show response
console.log(response);
}).catch(function(error) {
// Show error
console.log(error);
});
答案 0 :(得分:1)
如果要映射到此路线
/api/recordcoworkers/startdate/13
您需要像这样在您的HttpPost中添加id参数:
[HttpPost("StartDate/{id}")]
或者,如果您想使用/api/recordcoworkers/13/startdate
,则显然需要颠倒顺序:
[HttpPost("{id}/StartDate")]
您可以在Microsoft官方文档中阅读有关路由的更多信息: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.2#route-name