请求的资源不支持http方法';获取';调用返回布尔值的操作时

本文关键字:返回 布尔值 操作 调用 获取 方法 资源 请求 不支持 http | 更新日期: 2023-09-27 18:30:10

我有一个Web API,其中控制器中有多个get方法。其中一个控制器方法返回true或flas-用作员工id 的验证

以下是我的代码:

我的存储库类:

public class myRepository
{
    public myClasses.Type[] GetAllTypes()
    {
        return new myClasses.Type[]
        {
            new myClasses.Type 
            {
                typeId="1",
                typeVal = "New"
            },
            new myClasses.Type 
            {
                typeId="2",
                typeVal = "Old"
            }
       };
    }
    public myClasses.Employee[] GetAllEmployees()
    {
        return new myClasses.Employee[]
        {
            new myClasses.Employee 
            {
                empId="111111",
                empFName = "Jane",
                empLName="Doe"
            },
            new myClasses.Employee 
            {
                empId="222222",
                empFName = "John",
                empLName="Doe"
            }
       };
    }
    public bool VerifyEmployeeId(string id)
    {
        myClasses.Employee[] emp = new myClasses.Employee[]
        {
            new myClasses.Employee 
            {
                empId="111111",
                empFName = "Jane",
                empLName="Doe"
            },
            new myClasses.Employee 
            {
                empId="222222",
                empFName = "John",
                empLName="Doe"
            }
       };
        for (var i = 0; i <= emp.Length - 1; i++)
        {
            if (emp[i].empId == id)
                return true;
        }
        return false;
    }
}

和我的模型类:

public class myClasses
{
    public class Employee
    {
        public string empId { get; set; }
        public string empFName { get; set; }
        public string empLName { get; set; }
    }
    public class Type
    {
        public string typeId { get; set; }
        public string typeVal { get; set; }
    }
}

这是我的控制器:

public class myClassesController : ApiController
{
    private myRepository empRepository;
    public myClassesController()
    {
        this.empRepository = new myRepository();
    }
    public myClasses.Type[] GetTypes()
    {
        return empRepository.GetAllTypes();
    }
    public myClasses.Employee[] GetEmployees()
    {
        return empRepository.GetAllEmployees();
    }
    public bool VerifyEmployee(string id)
    {
        return empRepository.VerifyEmployeeId(id);
    }
}

一切都编译得很好,但当我使用运行它时

http://localhost:49358/api/myClasses/GetTypes

并以xml格式返回所有数据。但当我运行时

http://localhost:49358/api/myClasses/VerifyEmployee/222222

我收到一个错误"请求的资源不支持http方法'get'"

我的WebAPIConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional });
        // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
        // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
        // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
        //config.EnableQuerySupport();
        // To disable tracing in your application, please comment out or remove the following line of code
        // For more information, refer to: http://www.asp.net/web-api
        config.EnableSystemDiagnosticsTracing();
    }
}

有人能给我指正确的方向吗?我在这里做错了什么?是否有更好的方法通过Web API对数据进行验证?

请求的资源不支持http方法';获取';调用返回布尔值的操作时

在我看来,你不需要创建一个Action来验证你的员工,当你想对这个员工做任何更改时,你可以在这个Action中验证它,这样你就可以保存1个额外的请求,例如:

[HttpPut]
public IHttpActionResult Update(EmployeeViewModel model)
{
    if(!empRepository.VerifyEmployeeId(model.Id))
        return BadRequest("Some error message");
    empRepository.Update(model);
    return Ok();
}

在您的返回类型中,您应该使用IHttpActionResult来使用Http状态代码。除非您需要退回Collection。

如果出于任何原因,你只需要一个操作来验证员工,你可以这样做:

[HttpGet]
public IHttpActionResult VerifyEmployee(string id)
{
    var isValid = empRepository.VerifyEmployeeId(string id);
    return isValid ? Ok() : BadRequest("Some message");
}

如果您使用的是WebApi版本1,请尝试以下操作:

[HttpPut]
public HttpResponseMessage Update(EmployeeViewModel model)
{
    if(!empRepository.VerifyEmployeeId(model.Id))
        return new HttpResponseMessage(HttpStatusCode.BadRequest);
    empRepository.Update(model);
    return new HttpResponseMessage(HttpStatusCode.OK);
}
[HttpGet]
public HttpResponseMessage VerifyEmployee(string id)
{
    var isValid = empRepository.VerifyEmployeeId(string id);
    return isValid ? new HttpResponseMessage(HttpStatusCode.OK) : new HttpResponseMessage(HttpStatusCode.BadRequest);
}