gpt4 book ai didi

c# - Web API 中的多个操作(差异名称、http 动词)的一条路线

转载 作者:太空宇宙 更新时间:2023-11-03 15:51:40 25 4
gpt4 key购买 nike

我想为不同的操作创建一条路线,例如: http://www.example.com/Students

我在 StudentController 中有一些操作:

[HttpDelete]
public Student DeleteStudent(int id)

[HttpPost]
public int AddStudent(StudentInfo student)

[HttpPut]
public bool UpdateStudent(StudentInfo student)

我想创建一个路由,它根据请求的 Http 方法确定将调用什么操作。

所以当用户调用http://www.example.com/Students通过 Post 方法,它将是 AddStudent

最佳答案

试试这个

  // Get:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Students()
{
// do some stuff
return View();
}

// Post:
[ActionName("Students")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Students_Post()
{
// do some stuff
return View();
}

关于c# - Web API 中的多个操作(差异名称、http 动词)的一条路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25518661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com