c# - HttpClient PutAsync 未更新 Web API
问题描述
我有一个简单的 .Net Core Web API,带有一些简单的 Get、post、put 端点。我还创建了一个简单的控制台客户端来测试 API。get 和 post 方法工作正常,但当我测试 Put 时,它不会使用新数据更新 Web API。我已经用 swagger 测试了 Web API Put 方法,它工作正常,所以我的控制台应用程序客户端逻辑似乎不正确。谁能看到我做错了什么?
编辑:天气是一个内存集合 - 一个列表,所以我不需要 SaveChanges()
控制台客户端:
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
namespace Lab1RestConsole
{
class Program
{
private static readonly HttpClient client = new HttpClient();
private static readonly String baseURI = "https://localhost:44347/api/Weather/";
static async Task RunAsync()
{
try
{
//1 GET /api/Weather/
var serializer = new DataContractJsonSerializer(typeof(List<WeatherInformation>));
var streamTask = client.GetStreamAsync(baseURI);
var entries = serializer.ReadObject(await streamTask) as List<WeatherInformation>;
foreach (var e in entries)
{
Console.WriteLine(e.ToString());
}
Console.WriteLine("");
//2 GET /api/Weather/
serializer = new DataContractJsonSerializer(typeof(List<WeatherInformation>));
streamTask = client.GetStreamAsync(baseURI + "warning/false");
entries = serializer.ReadObject(await streamTask) as List<WeatherInformation>;
foreach (var e in entries)
{
Console.WriteLine(e.ToString());
}
//3 GET /api/Weather/city/Dublin
serializer = new DataContractJsonSerializer(typeof(WeatherInformation));
streamTask = client.GetStreamAsync(baseURI + "city/Dublin");
WeatherInformation weatherinfo = serializer.ReadObject(await streamTask) as WeatherInformation;
Console.WriteLine("\n" + weatherinfo + "\n");
// serialise to JSON
weatherinfo = new WeatherInformation() { City = "Antwerp", Temp = 3, Warning = true, WeatherType = 0, WindSpeed = 22 }; ;
MemoryStream ms = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(WeatherInformation));
ser.WriteObject(ms, weatherinfo);
// read and create string content for POST
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
StringContent content = new StringContent(sr.ReadToEnd(), Encoding.UTF8, "application/json");
//4 POST - add new city
HttpResponseMessage httpResponse = await client.PostAsync(baseURI, content);
httpResponse.EnsureSuccessStatusCode();
//5 update PUT to api/Weather/Dublin - now temp is 10
weatherinfo = new WeatherInformation() { City = "Antwerp", Temp = 10, Warning = true, WeatherType = 0, WindSpeed = 22 }; ;
var putTask = client.PutAsync(baseURI + "Dublin", content);
}
catch (HttpRequestException e)
{
Console.WriteLine(e.Message);
}
}
static void Main()
{
RunAsync().Wait();
client.Dispose();
Console.ReadLine();
}
}
}
Web API 中的 Put 操作
// PUT: api/Weather/5
[HttpPut("{city}")]
public IActionResult PutUpdateCityWeather(string city, WeatherInformation weatherInfo)
{
if (ModelState.IsValid)
{
var record = weather.FirstOrDefault(e => e.City.ToUpper() == city.ToUpper());
if (record != null)
{
record.Temp = weatherInfo.Temp;
record.Warning = weatherInfo.Warning;
record.WeatherType = weatherInfo.WeatherType;
record.WindSpeed = weatherInfo.WindSpeed;
return Ok();
}
else
{
return BadRequest();
}
}
else
{
return BadRequest(ModelState);
}
}
解决方案
推荐阅读
- python - 显示检索到的文档总数
- asp.net - ASP.Net Core app.UseStaticFiles 在生产 (Azure) 中不起作用,对静态文件的调用下降到默认路由
- python - DEAP 中的 PyGame/PyMunk 评估问题
- c++ - 将 std::array 推送到 std::stack
- dokku - 如何在我的 dokku 主机中使用自定义二进制文件?
- css - Bootstrap 4:获取主菜单容器的全宽下拉菜单
- ios - 需要帮助在 UITableViewCell 中使用动画制作进度
- flutter - Firestore 要求我创建已经存在的索引
- swift - 无法使用可编码的通用参数调用函数
- jpa - SpringData JPA 事务管理