【问题标题】:Send values from Windows Forms Application to Asp.Net Core Api将值从 Windows 窗体应用程序发送到 Asp.Net Core Api
【发布时间】:2020-10-05 12:17:04
【问题描述】:

我已经创建了一个简单的 Asp.Net Api 来在 Winforms 应用程序中显示学生的信息,但是,现在我想要创建一个从 Windows Forms 应用程序接受值并将这些值插入数据库的 Api。我怎么能创造这样的东西?这是我到目前为止所尝试的:

Asp.Net API:

[Route("api/[controller]")]
[ApiController]
public class Students : ControllerBase
{
 
     SqlConnection con;
     SqlCommand cmd;

    [HttpPost]
    public void Post()
    {
        string name = "janet";
        string age = "12";
         
        con = new SqlConnection("ConnectionString");
        cmd = new SqlCommand("insert into People(name,age) values(@name,@age)", con);
        cmd.Parameters.AddWithValue("@name", name);
        cmd.Parameters.AddWithValue("@age", age);
        con.Open();
        cmd.ExecuteNonQuery();
    }

}

Windows 窗体应用程序:

    private void button1_Click(object sender, EventArgs e)
    {
        action();
    }
    HttpResponseMessage response;
    HttpClient client;
    async void action()
    {
            client = new HttpClient();   
  
            client.BaseAddress = new Uri("https://localhost:44338/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            response = await client.GetAsync("api/Students");
            
            if (response.IsSuccessStatusCode)
            {
                MessageBox.Show("item added successfully");

            }

        }
    

我想将 Api 中的 name 和 age 变量更改为我在 Winforms 中输入的任何值。

PS:我知道我在 winforms 中使用了client.GetAsync,它应该是client.PostAsync,但我不知道应该传递什么参数。

感谢您的帮助。

【问题讨论】:

  • 您应该阅读You're using HttpClient wrong and it's destabilizing your software,因为您正在破坏那里的关键指南之一。您可能对Flurl 有更好的体验,它的语法更简洁,并为您处理了一些棘手的问题。
  • 您应该避免使用async void,除了事件处理程序本身。您的操作方法应该返回一个任务,然后 button1_Click 应该等待它,因此 button1_Click 必须标记为异步。

标签: c# asp.net winforms api asp.net-core


【解决方案1】:

您可以在PostAsync方法的第二个参数中以StringContent的形式传递数据。

你可以把这些你需要的参数传递给一个模型,并将它们转换成json到控制器。

这里是演示:

您可以在这两个应用程序中创建Person类(方便传输和接收数据):

 public class Person
    {
        public int Id { get; set; }
        public string name { get; set; }
        public int age { get; set; }
    }

Windows 窗体应用程序:

 using (var client = new HttpClient())
        {

            client.BaseAddress = new Uri("https://localhost:44338/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
              
               Person  person = new Person() {  name= "janet", age =12};
                var data = JsonConvert.SerializeObject(person);
                var content = new StringContent(data, Encoding.UTF8, "application/json");
                response = await client.PostAsync("api/Students", content);
           

            if (response.IsSuccessStatusCode)
            {
                MessageBox.Show("item added successfully");

            }

        }

控制器接口:

 [HttpPost]
        public void Post([FromBody]Person person)
        { 
            con = new SqlConnection("ConnectionString");
            cmd = new SqlCommand("insert into People(name,age) values(@name,@age)", con);
            cmd.Parameters.AddWithValue("@name", person.name);
            cmd.Parameters.AddWithValue("@age", person.age);
            con.Open();
            cmd.ExecuteNonQuery();
        }

【讨论】:

猜你喜欢
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-12
  • 1970-01-01
  • 1970-01-01
  • 2011-06-05
  • 1970-01-01
相关资源
最近更新 更多