为什么我应该使用 get 而不是 post?

时间:2021-07-01 17:31:55

标签: c# web-services http-post http-get

我有一个用 mvc c# 开发的 web 服务,该服务的目的是检索和插入数据到 sql。所有方法都使用 http 属性,所以我的问题是,如果所有方法都使用 [HttpPost] 并且工作正常,我应该什么时候使用 [HttpGet]。

    [HttpPost]
    [Route("GetVehiculos")]
    public HttpResponseMessage WSGetVehiculos()
    {
        VehiculosDAO vehiculosDAO = new VehiculosDAO();
        List<Vehiculo> lstVehiculo = new List<Vehiculo>();
        string codError = "0";
        string mensaje = "";
        try
        {
            lstVehiculo = vehiculosDAO.GetVehiculos();
            
            mensaje = "OK";
        }
        catch (Exception ex)
        {
            codError ="1";
            Log.WriteToFileLogGeneral(ex.Message);

        }

        return Request.CreateResponse(HttpStatusCode.OK, new { Msg = mensaje, CodError = 
        codError,Data= lstVehiculo } );
    }

提前致谢

1 个答案:

答案 0 :(得分:1)

GET 用于查看某些内容,而不对其进行更改,而 POST 用于更改某些内容。例如,搜索页面应该使用 GET 来获取数据,而更改密码的表单应该使用 POST。本质上GET用于检索远程数据,POST用于插入/更新远程数据。