本文介绍了如何向 ASP.NET WebAPI 控制器添加自定义方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 ASP.NET MVC WebAPI 项目中,我们默认创建了以下控制器
In ASP.NET MVC WebAPI project by default we have created following controller
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
但是可以在这里添加任何自定义方法,以便它们也可以支持 get/post 吗?
But is possible to add here any custom methods so they can support get/post as well?
谢谢!
推荐答案
您可以将 RoutePrefix 等属性与 Http 类型一起使用.
You can use attributes such as the RoutePrefix with the Http type.
[Route("ChangePassword")]
[HttpPost] // There are HttpGet, HttpPost, HttpPut, HttpDelete.
public async Task<IHttpActionResult> ChangePassword(ChangePasswordModel model)
{
}
http 类型将结合路由名称将其映射回正确的方法.
The http type will map it back to its correct method in combination with the Route name.
这篇关于如何向 ASP.NET WebAPI 控制器添加自定义方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!