【问题标题】:Where can I see the request data in an ASP.NET Core webservice?在哪里可以看到 ASP.NET Core Web 服务中的请求数据?
【发布时间】:2020-12-20 23:23:43
【问题描述】:

我发送了 12345678,但在 Visual Studio 中找不到。

我正在学习本教程。 enter link description here

我从 POSTMAN 发送“TemperatureCblah”:“12345678”,并希望以某种方式在 VStudio 中看到它。我知道那有点原始,但我说的是步行/跑步。

在 Visual Studio 中,我到达了这个断点,但我在任何地方都找不到 12345678....

谢谢!

【问题讨论】:

  • 看看 HttpContext.Request.Body

标签: json asp.net-core postman


【解决方案1】:

由于您的操作是用 [HttpGet] 装饰的,因此在您的 (GET) 请求中发送正文是没有意义的。

您可以使用查询参数将值发送到 Get Action,并且您将能够使用如下简单参数获取它们:

Get /weatherforecast?temperatureCblah=12345678

[HttpGet]
public IEnumerable<WeatherForecast> Get(string temperatureCblah)
{
    ...
}

对于 POST 请求,您可以进行 HttpPost 修饰操作,然后您可以填充请求主体,该主体将被自动解析为对象模型。

[HttpPost]
public void Post(WeatherForecast weatherForecast)
{
    ...
}

【讨论】:

  • 我使用了你的代码,但手表显示它为 temperatureCblah= null
【解决方案2】:

定义一个模型,包含你想从 json 接受的字段。

public class TestModel
{
    public string TemperatureCblah { get; set; }
    public string sl { get; set; }
    public string tl { get; set; }
}

然后将其作为参数接收json数据。

[HttpGet]
public IEnumerable<WeatherForecast> GetAsync(TestModel testModel)
{
    //some code
}

邮递员发送请求:

结果:

【讨论】:

    猜你喜欢
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多