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

标签 c# asp.net-mvc

我想为不同的操作创建一条路线,例如: 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/

相关文章:

c# - 从 3D 中的对角线计算矩形的点

c# - 需要名字姓氏程序的帮助

c# - .Net WebApi 中的多个等待异步

c# - 请求 URL "HelloWorld"如何在 ASP.Net MVC 中实例化 HelloWorldController 对象?

asp.net-mvc - Azure 构建配置与服务配置

asp.net-mvc - Asp.net Mvc : List all the actions on a controller with specific attribute

c# - 如何使用 AutoMapper 模拟列表转换

c# - 在 C# 中用一个 ForEach 语句迭代两个列表或数组

asp.net-mvc - asp.net mvc razor 将两项相乘并转换为字符串

asp.net-mvc - C# foreach within foreach,每项显示3项。旋转木马