0

(VS2019 Preview 2 / Core 3 Preview 8)当我在控制器中设置断点时,它被击中,但是“text”作为null(我期望“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());
...
4

1 回答 1

0

试试看

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");
//...
于 2019-08-22T01:57:27.870 回答