如果需要传递数据请进行序列化操作:
//例如: //序列化 user user = new user(); user.userName= "admin"; user.password = "123456"; var str = JsonConvert.SerializeObject(user);
post传参案例
注:【传参无非就是以【?&】字符串拼接后执行】
[HttpPost] public void UpdateInfo(string id,string nickName,string introduce) { //请求地址 string url = string.Format("http://localhost:5534/api/Test/UpdateById?id={0}&nickName={1}&introduce={2}", id, nickName, introduce); //发送值前台 Success success = RequestPost(url); //根据请求返回判断结果 if (success.code == 200) { Response.Write("<script>alert('" + success.message + "');window.location.href='Index'</script>"); } else { Response.Write("<script>alert('" + success.message + "');window.location.href='UpdatePage'</script>"); } } /// <summary> /// Post /// </summary> /// <param name="url"></param> /// <returns></returns> public Success RequestPost(string url) { //客户端请求 HttpClient http = new HttpClient(); var str = "";//请求数据。这里为空 HttpContent content = new StringContent(str); //请求地址 Task<HttpResponseMessage> postTask = http.PostAsync(url, content); HttpResponseMessage result = postTask.Result;//拿到网络请求结果 result.EnsureSuccessStatusCode();//抛出异常 Task<string> task = result.Content.ReadAsStringAsync();//异步读取数据 //发送值前台 return JsonConvert.DeserializeObject<Success>(task.Result); }
希望能对大家有所帮助。
第五章【五、ASP.NET MVC (五、HttpClient接口解析)】完结。
【一、控制器与视图:https://laoshifu.blog.csdn.net/article/details/120126288】
【二、数据传递:https://laoshifu.blog.csdn.net/article/details/120127320】
【三、表单与文件上传:https://laoshifu.blog.csdn.net/article/details/120130566】
【四、ASP.NET Web API应用程序与跨域操作:https://laoshifu.blog.csdn.net/article/details/120160728】
【五、HttpClient接口解析:https://laoshifu.blog.csdn.net/article/details/120332379】