如何自定义方法添加到ASP.NET的WebAPI控制器? [英] How to add custom methods to ASP.NET WebAPI controller?
本文介绍了如何自定义方法添加到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?
感谢您!
推荐答案
您可以使用属性,如使用HTTP类型的路线preFIX。
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控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文