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

时间:2017-01-08 17:00:22

标签: c# asp.net-mvc asp.net-web-api asp.net-web-api-routing

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

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

控制器代码

    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请求访问UpdateEmai l方法和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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

您的WebApConig.cs应如下所示 -

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

然后发出类似 -

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