88 lines
2.1 KiB
C#
88 lines
2.1 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace NBF;
|
|
|
|
public abstract class NBControllerBase : ControllerBase
|
|
{
|
|
protected static readonly string BaseRootPath = $"{AppContext.BaseDirectory}wwwroot/";
|
|
|
|
|
|
public IActionResult Html(string fileName)
|
|
{
|
|
var filePath = Path.Combine(BaseRootPath, $"{fileName}.html");
|
|
|
|
if (System.IO.File.Exists(filePath))
|
|
{
|
|
return PhysicalFile(filePath, "text/html");
|
|
}
|
|
|
|
return Content($"{fileName}.html not found");
|
|
}
|
|
|
|
public OkObjectResult Error(ErrorCode code, string msg = "")
|
|
{
|
|
var res = new ResponseData<string>
|
|
{
|
|
Code = (int)code,
|
|
Data = msg
|
|
};
|
|
return Ok(res);
|
|
}
|
|
|
|
public OkObjectResult Success()
|
|
{
|
|
var res = new ResponseData<string>
|
|
{
|
|
Code = 0,
|
|
Data = string.Empty
|
|
};
|
|
return Ok(res);
|
|
}
|
|
|
|
public OkObjectResult Success<T>(T data)
|
|
{
|
|
var res = new ResponseData<T>
|
|
{
|
|
Code = 0,
|
|
Data = data
|
|
};
|
|
return Ok(res);
|
|
}
|
|
|
|
#region 工具方法
|
|
|
|
/// <summary>
|
|
/// 获得请求url参数
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
protected Dictionary<string, string> GetQuery()
|
|
{
|
|
Dictionary<string, string> paramMap = new Dictionary<string, string>();
|
|
var request = HttpContext.Request;
|
|
foreach (var keyValuePair in request.Query)
|
|
paramMap.Add(keyValuePair.Key, keyValuePair.Value[0]);
|
|
|
|
return paramMap;
|
|
}
|
|
|
|
protected Dictionary<string, string> GetHeaders()
|
|
{
|
|
Dictionary<string, string> paramMap = new Dictionary<string, string>();
|
|
var request = HttpContext.Request;
|
|
foreach (var keyValuePair in request.Headers)
|
|
paramMap.Add(keyValuePair.Key, keyValuePair.Value[0]);
|
|
|
|
return paramMap;
|
|
}
|
|
|
|
protected static void TryCreateDir(string path)
|
|
{
|
|
if (string.IsNullOrEmpty(path)) return;
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Directory.CreateDirectory(path);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
} |