提交功能
This commit is contained in:
49
Http/Extensions/HttpContextExtension.cs
Normal file
49
Http/Extensions/HttpContextExtension.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace ACBuildService;
|
||||
|
||||
public static class HttpContextExtension
|
||||
{
|
||||
public static async Task Error(this HttpContext context, ErrorCode code, string msg = "")
|
||||
{
|
||||
var res = new ResponseData<string>
|
||||
{
|
||||
Code = (int)code,
|
||||
Data = msg
|
||||
};
|
||||
await context.WriteJson(res);
|
||||
}
|
||||
|
||||
public static async Task Success(this HttpContext context)
|
||||
{
|
||||
var res = new ResponseData<string>
|
||||
{
|
||||
Code = 0,
|
||||
Data = string.Empty
|
||||
};
|
||||
await context.WriteJson(res);
|
||||
}
|
||||
|
||||
public static async Task Success<T>(this HttpContext context, T data)
|
||||
{
|
||||
var res = new ResponseData<T>
|
||||
{
|
||||
Code = 0,
|
||||
Data = data
|
||||
};
|
||||
await context.WriteJson(res);
|
||||
}
|
||||
|
||||
public static async Task WriteJson<T>(this HttpContext context, T obj)
|
||||
{
|
||||
context.Response.ContentType = "application/json";
|
||||
if (obj is string st)
|
||||
{
|
||||
await context.Response.WriteAsync(st);
|
||||
}
|
||||
else
|
||||
{
|
||||
await context.Response.WriteAsync(JSON.Serialize(obj));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user