WebAPI 异步任务<IHttpActionResult> 传递模型
WebAPI async Task<IHttpActionResult> passing model
这是我的控制器
public class TutorController : ApiController
{
[Route("CreateTutor")]
public async Task<IHttpActionResult> CreateTutor(TutorModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
return Ok();
}
}
我正在使用 Fiddler 连接它
POST http://localhost:12110/api/Tutor/CreateTutor
我设置了 raw 和 application/application
在Body我有
{
"Name": "Test"
}
但是我得到这个错误
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:12110/api/Tutor/CreateTutor'.",
"MessageDetail": "No action was found on the controller 'Tutor' that matches the request."
}
知道我做错了什么吗?
API 控制器中的每个方法都必须有一个属性来确定它
您的方法中缺少 action 属性,例如 eg。以下
[Route("api/[controller]")]
[ApiController]
public class TutorController : ApiController
{
[HttpPost]
[Route("CreateTutor")]
public async Task<IHttpActionResult> CreateTutor(TutorModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
return Ok();
}
}
在 MS 文档中阅读更多信息:
https://docs.microsoft.com/en-US/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#http-methods
这是我的控制器
public class TutorController : ApiController
{
[Route("CreateTutor")]
public async Task<IHttpActionResult> CreateTutor(TutorModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
return Ok();
}
}
我正在使用 Fiddler 连接它
POST http://localhost:12110/api/Tutor/CreateTutor
我设置了 raw 和 application/application
在Body我有
{
"Name": "Test"
}
但是我得到这个错误 { "Message": "No HTTP resource was found that matches the request URI 'http://localhost:12110/api/Tutor/CreateTutor'.", "MessageDetail": "No action was found on the controller 'Tutor' that matches the request." }
知道我做错了什么吗?
API 控制器中的每个方法都必须有一个属性来确定它
您的方法中缺少 action 属性,例如 eg。以下
[Route("api/[controller]")]
[ApiController]
public class TutorController : ApiController
{
[HttpPost]
[Route("CreateTutor")]
public async Task<IHttpActionResult> CreateTutor(TutorModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
return Ok();
}
}
在 MS 文档中阅读更多信息: https://docs.microsoft.com/en-US/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#http-methods