You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.8 KiB
C#

9 months ago
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace HttpClientStudy.WebApp.Controllers
{
/// <summary>
/// 高级Get请求 控制器
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class AdvancedGetController : ControllerBase
{
private ILogger<SimpleController> _logger;
/// <summary>
/// 构造
/// </summary>
public AdvancedGetController(ILogger<SimpleController> logger)
{
_logger = logger;
}
/// <summary>
/// 带请求体数据的Get请求
/// (asp.net 3开始默认不允许Get有Body)
9 months ago
/// 注意:不能直接使用模型绑定接收数据,而应以流的方式读取请求体中的数据
9 months ago
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<IActionResult> GetWithBodyAsync()
{
9 months ago
var requestBody = "";
//以流方式读取请求体中的数据
9 months ago
if (Request.ContentLength>0)
9 months ago
{
9 months ago
using (var reader = new StreamReader(Request.Body, Encoding.UTF8, detectEncodingFromByteOrderMarks: false, bufferSize: 1024, leaveOpen: true))
{
9 months ago
requestBody = await reader.ReadToEndAsync();
9 months ago
// 现在你有了请求体,可以按照你的需求处理它
}
9 months ago
}
9 months ago
var result = BaseResultUtil.Success(requestBody);
return Ok(result);
}
9 months ago
9 months ago
/// <summary>
/// Post测试方法
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult PostDemo()
{
var result = BaseResultUtil.Success("操作成功");
return Ok(result);
9 months ago
}
}
}