gpt4 book ai didi

c# - 如何在 WebApi 中使用多个 GET 操作?

转载 作者:行者123 更新时间:2023-11-30 15:19:51 25 4
gpt4 key购买 nike

好吧,我实际上正在尝试学习 WebApi,假设我有一个场景,我有两个像这样的 get 方法

Controller

public class EmployeeApiController : ApiController
{
public List<Student> GetAllStudents() { ... }

public List<Student> EmailChange(string studentName, string Email) { ... }

public List<Student> AddressChange(string studentName, string Address) { ... }
}

public class Student
{
public string StudentName { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public static List<Student> students { get; set; }
}

我无法调用相应的方法,我该怎么做,我知道有很多博客,但它并没有帮助我理解如何真正访问这些方法。通过浏览几个博客,我将我的整个代码变成了这样

WebApiConfig代码

public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}

Controller 代码

    public List<Student> GetAllStudents()
{
Student.students = new List<Student> {
new Student { StudentName="foo",Address="usa",Email="foo@yahoo.com"},
new Student { StudentName="joe",Address="mumbai",Email="joe@yahoo.com"},
new Student { StudentName="albert",Address="georgia",Email="albert@yahoo.com"},
new Student { StudentName="prince",Address="missisipi",Email="prince@yahoo.com"}
};
return Student.students;
}
[HttpGet]
public List<Student> UpdateEmail(string studentName, string Email)
{
return Student.students.Select(i =>
{
if (i.StudentName == studentName)
{
i.Email = Email;
}
return i;
}).ToList();

}
[HttpGet]
public List<Student> UpdateAddress(string studentName, string Address)
{
return Student.students.Select(x =>
{
if (x.StudentName == studentName)
{
x.Address = Address;
}
return x;
}).ToList();
}
}

public class Student
{
public string StudentName { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public static List<Student> students { get; set; }
}

我不太清楚如何使用 GET 请求访问 UpdateEmail 方法和 UpdateAddress 方法。

更新 1

当我这样打电话的时候

http://localhost:53711/api/EmployeeApi/UpdateAddress

http://localhost:53711/api/EmployeeApi/UpdateEmail

我得到这样的错误

enter image description here

当我像这样打电话时,我得到了这样的错误

http://localhost:53711/api/EmployeeApi/UpdateEmail/foo/foo enter image description here

最佳答案

要么将模板更改为 routeTemplate: "api/{controller}/{action}/{studentName}",并保留方法

public static void Register(HttpConfiguration config) {
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{studentName}"
);
}

保留模板原样,即:"api/{controller}/{action}/{id}" 并将方法参数更改为 (string id,...... .)

[HttpGet]
public List<Student> UpdateEmail(string id, string Email) { ... }

您也可以放弃基于约定的路由并使用属性路由

[RoutePrefix("api/EmployeeApi")]
public class EmployeeApiController : ApiController
{
//GET api/EmployeeApi
[HttpGet]
[Route("")]
public List<Student> GetAllStudents() { ... }

//GET api/EmployeeApi/EmailChange/foo/foo@email.com
[HttpGet]
[Route("EmailChange/{studentName}/{email}")]
public List<Student> EmailChange(string studentName, string email) { ... }

//GET api/EmployeeApi/AddressChange/foo/China
[HttpGet]
[Route("AddressChange/{studentName}/{address}")]
public List<Student> AddressChange(string studentName, string Address) { ... }
}

关于c# - 如何在 WebApi 中使用多个 GET 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41535274/

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