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

How to use multiple GET action in a WebApi?

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

控制器

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 }
    );
}

控制器代码

    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

我收到这样的错误

当我像这样打电话时,我收到类似

的错误

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

您的 WebApConig.cs 应该如下所示 -

api/{controller}/{action}/{id}

然后发出像 -

这样的调用
http://localhost:port/api/Ctrl/action

将模板更改为 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) { ... }
}