src/YL.Utils/Http/HttpExtensions.cs
(开头部分) 6KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.WebUtilities;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using YL.Utils.Extensions;
using YL.Utils.Json;
namespace YL.Utils.Http
{
public static class HttpExtensions
{
private static readonly JsonSerializer Serializer = new JsonSerializer();
public static Task WriteJson<T>(this HttpResponse response, T obj)
{
response.ContentType = "application/json";
return response.WriteAsync(JsonConvert.SerializeObject(obj));
}
public static void WriteJson<T>(this HttpResponse response, T obj, string contentType = null)
{
response.ContentType = contentType ?? "application/json";
using (var writer = new HttpResponseStreamWriter(response.Body, Encoding.UTF8))
{
using (var jsonWriter = new JsonTextWriter(writer))
{
jsonWriter.CloseOutput = false;
jsonWriter.AutoCompleteOnClose = false;
Serializer.Serialize(jsonWriter, obj);
}
}
}
public static Task WriteJson(this HttpResponse response, string str)
{
response.ContentType = "application/json";
return response.WriteAsync(str);
}
public static async Task<T> ReadFromJsonJil<T>(this HttpContext httpContext)
{
using (var streamReader = new StreamReader(httpContext.Request.Body))
{
var obj = streamReader.JilToObject<T>();
var results = new List<ValidationResult>();
if (Validator.TryValidateObject(obj, new ValidationContext(obj), results))
{
return obj;
}
httpContext.Response.StatusCode = 400;
await httpContext.Response.WriteJson(results.JilToJson());
return default;
}
}
后面还有 108 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。