简单方案中的 HttpPost 将 null 传递给控制器方法



(VS2019 预览版 2/酷睿 3 预览版 8( 当我在控制器中设置断点时,它被命中,但"文本"显示为空(我希望"foobar"(......我尝试了很多东西,你可以看到编码和应用程序类型被注释掉了......无论哪种情况,都是一样的。我是否缺少某些配置?

呼叫站点:

string text = "foobar";
HttpClient client = new HttpClient();
HttpContent content = new StringContent(text);//, Encoding.UTF8, "application/json");
client.PostAsync("http://localhost:49751/api/Data/PutDataAsync", content);

控制器:

[HttpPost("PutDataAsync")]
public void PutDataAsync(string text)
{
//a breakpoint here is hit, but "text" is null (should be "foobar")
}
OR
[HttpPost("PutDataAsync")]
public async Task PutDataAsync(string text)
{
//same result, text is null
}

启动.cs:

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddMvc().AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver());
...

试试吧

string text = "foobar";
var fooJSON = JsonConvert.SerializeObject(text); // trans to json string
HttpClient client = new HttpClient();
HttpContent content = new StringContent(fooJSON , Encoding.UTF8, "application/json");
//...

最新更新