Angular 11 Web API 控制器 POST 方法
Angular 11 Web API Controller POST method
我需要为我的 web API 控制器 class 创建 and/or 重用对象 class 到 post 这 4 个变量:
int Date, int TemperatureC, int TemperatureF, string Summary
我使用的是随 Angular 模板提供的默认天气预报控制器。我该如何使用目前掌握的信息创建 POST 方法?
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PROBLEM.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
public (int Date, int TemperatureC, int TemperatureF, string Summary) Posted { get; private set; }
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
[HttpPost]
public void Post()
{
}
}
}
您需要定义一个实体class来发送数据。Weather
如下所示。
public class Weather
{
public int TemperatureC {get;set;}
public int TemperatureF {get;set;}
public DateTime Date {get;set;}
public string Summary {get;set}
}
此外,您需要在 post 方法中从 body 接收此数据。
您的 post 方法如下所示。
private static List<Weather> weathers=new List<Weather>()
{
new Weather()
{TemperatureC=10,Summary="Chilli",TemperatureF=40,Date=DateTime.Now }
}
[HttpPost]
public ActionResult<List<Weather>> Post([FromBody] Weather weather)
{
//you recieve weather data and make your changes like adding db or list
//you can return new added value or changes in your db
weathers.Add(weather);
return weathers;
}
我需要为我的 web API 控制器 class 创建 and/or 重用对象 class 到 post 这 4 个变量:
int Date, int TemperatureC, int TemperatureF, string Summary
我使用的是随 Angular 模板提供的默认天气预报控制器。我该如何使用目前掌握的信息创建 POST 方法?
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PROBLEM.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
public (int Date, int TemperatureC, int TemperatureF, string Summary) Posted { get; private set; }
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
[HttpPost]
public void Post()
{
}
}
}
您需要定义一个实体class来发送数据。Weather
如下所示。
public class Weather
{
public int TemperatureC {get;set;}
public int TemperatureF {get;set;}
public DateTime Date {get;set;}
public string Summary {get;set}
}
此外,您需要在 post 方法中从 body 接收此数据。 您的 post 方法如下所示。
private static List<Weather> weathers=new List<Weather>()
{
new Weather()
{TemperatureC=10,Summary="Chilli",TemperatureF=40,Date=DateTime.Now }
}
[HttpPost]
public ActionResult<List<Weather>> Post([FromBody] Weather weather)
{
//you recieve weather data and make your changes like adding db or list
//you can return new added value or changes in your db
weathers.Add(weather);
return weathers;
}