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.
96 lines
2.7 KiB
C#
96 lines
2.7 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using System.Text;
|
|
|
|
using HttpClientStudy.WebApp.Models;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace HttpClientStudy.WebApp.Controllers
|
|
{
|
|
/// <summary>
|
|
/// 高级Post请求 控制器
|
|
/// </summary>
|
|
[Route("api/[controller]/[action]")]
|
|
[ApiController]
|
|
public class AdvancedPostController : ControllerBase
|
|
{
|
|
private ILogger<AdvancedPostController> _logger;
|
|
|
|
/// <summary>
|
|
/// 构造
|
|
/// </summary>
|
|
public AdvancedPostController(ILogger<AdvancedPostController> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
#region 接收请求体数据
|
|
|
|
/// <summary>
|
|
/// 接收请求体数据
|
|
/// 普通文本:直接从原始请求体获取。不能使用 [FromBody]绑定特性,因为绑定是针对 Form表单、Json格式的特殊数据。
|
|
/// </summary>
|
|
/// <returns>
|
|
/// 返回请求体原始数据
|
|
/// </returns>
|
|
[HttpPost]
|
|
public async Task<IActionResult> TextData()
|
|
{
|
|
string content = "请求体没有数据";
|
|
|
|
if (Request.ContentLength>0)
|
|
{
|
|
byte[] bytes = new byte[(int)Request.ContentLength];
|
|
|
|
await Request.Body.ReadAsync(bytes);
|
|
|
|
content = UnicodeEncoding.UTF8.GetString(bytes);
|
|
}
|
|
|
|
var result = BaseResultUtil.Success(content);
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 接收请求体数据
|
|
/// 表单数据
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public IActionResult FormData([FromForm,Required]int id, [FromForm,Required]string name)
|
|
{
|
|
var paras = $"{nameof(id)}={id}&{nameof(name)}={name}";
|
|
var result = BaseResultUtil.Success(paras);
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 接收请求体数据
|
|
/// 编码后的表单数据
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public IActionResult FormUrlEncodedData([FromForm, Required] int id, [FromForm, Required] string name)
|
|
{
|
|
var paras = $"{nameof(id)}={id}&{nameof(name)}={name}";
|
|
var result = BaseResultUtil.Success(paras);
|
|
return Ok(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 接收请求体数据
|
|
/// json 数据
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public IActionResult JsonData([FromBody]AdvancedGetModel? vm)
|
|
{
|
|
var result = BaseResultUtil.Success(vm);
|
|
return Ok(result);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|