将参数(字符串)传递给GetAsync()?

时间:2017-03-24 15:34:23

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

我是创建Web Api的新手,我需要将字符串传递给GetAsync方法,但我不知道应该在WebApiConfig文件中添加什么以及更改后如何在GetAsync上添加。我在这里搜索过,但我发现没有任何帮助我的东西。有人可以帮帮我吗?

我的程序:

private async void registrarServico(string nomeServico)
{
    using (HttpClient clientGet = new HttpClient())
    using (HttpClient clientSet = new HttpClient())
    {
        clientGet.BaseAddress = new Uri("api/ControladorServico/GetServico");
        var respostaGet = await clientGet.GetAsync("", nomeServico); //ERROR HERE!
    }
}

我的WebApiConfig:

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

我的API方法:

[HttpGet]
public IHttpActionResult GetServico(string nomeServico)
{
    try
    {
        return Ok(controladorServicoRep.CONTROLADOR_SERVICOSep.Get(d => d.NOME == nomeServico && d.MAQUINA == nomeMaquina).FirstOrDefault());
    }
    catch (Exception)
    {
        throw;
    }
}

谢谢。

1 个答案:

答案 0 :(得分:3)

做这样的事情:

conn = new MySqlConnection("server=" + hostname + ";uid=" + username + ";pwd=" + password + ";database=databaseimage;Charset=latin1;");
            conn.Open();
            FileStream fs;
            Byte[] bindata;
            MySqlParameter picpara;
            cmd = new MySqlCommand("INSERT INTO mypic (pic) VALUES(?pic)", conn);
            picpara = cmd.Parameters.Add("?pic", MySqlDbType.MediumBlob);
            cmd.Prepare();

//txtPicPath is the path of the image, e.g. C:\MyPic.png

            fs = new FileStream(txtPicPath.Text, FileMode.Open, FileAccess.Read);
            bindata = new byte[Convert.ToInt32(fs.Length)];
            fs.Read(bindata, 0, Convert.ToInt32(fs.Length));
            fs.Close();

            picpara.Value = bindata;
            cmd.ExecuteNonQuery();

然后你可以将其称为:

if (conn == null) // Just to make sure that the connection was not severed
        {


                conn = new MySqlConnection("server=" + hostname + ";uid=" + username + ";pwd=" + password + ";database=databaseimage;Charset=latin1;");
                conn.Open();

        }
        MemoryStream ms = new MemoryStream();
        FileStream fs;
        Byte[] bindata;

        cmd = new MySqlCommand("SELECT pic FROM mypic WHERE id=3", conn);
        bindata = (byte[])(cmd.ExecuteScalar());



        ms.Write(bindata, 0, bindata.Length);
        pb2.Image = new Bitmap(ms);

        fs = new FileStream(name, FileMode.Create, FileAccess.Write);
        ms.WriteTo(fs);
相关问题