using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Configuration; using System.Diagnostics; using System.IO; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace DW5S.WebApi { /// /// /// [ApiController] [Route("api/[controller]/[action]")] public class BaseController : ControllerBase { private readonly string uploadFolder; /// /// /// public BaseController() { this.uploadFolder =AppDomain.CurrentDomain.BaseDirectory; } /// /// 发布为单文件App时确保可以找到Controller程序集 /// [NonAction] internal static void EnsureAssemblyLoaded() { } /// /// 返回本地文件 /// /// /// protected string GetLocalFile(string fileName) { return Path.Combine(this.uploadFolder, fileName); } /// /// 获取客户端IP地址 /// protected string RemoteIp { get { var ip = HttpContext.Connection.RemoteIpAddress?.ToString(); if (ip == null || !ip.Contains(".")) { return "127.0.0.1"; } var idx = ip.LastIndexOf(":"); if (idx >= 0) { ip = ip.Substring(idx + 1); } return ip; } } /// /// 获取客户端使用的连接端口 /// protected int RemotePort { get { return HttpContext.Connection.RemotePort; } } /// /// 获取本地IP地址 /// protected string LocalIp { get { var ip = HttpContext.Connection.LocalIpAddress?.ToString(); if (ip == null || !ip.Contains(".")) { return "127.0.0.1"; } var idx = ip.LastIndexOf(":"); if (idx >= 0) { ip = ip.Substring(idx + 1); } return ip; } } /// /// 获取本地使用的http端口 /// protected int LocalPort { get { return HttpContext.Connection.LocalPort; } } /// /// 返回成功 /// /// 返回的数据 /// protected AjaxResult Success(object data = null) { AjaxResult res = new AjaxResult { code = 200, msg = "请求成功!", data = data }; return res; } /// /// 返回成功 /// /// 返回的消息 /// 返回的数据 /// protected AjaxResult Success(object data, string msg = "请求成功!") { AjaxResult res = new AjaxResult { code = 200, msg = msg, data = data }; return res; } /// /// 返回成功 /// /// 返回的消息 /// 返回的数据 /// protected AjaxResult Success(T data, string msg = "请求成功!") { AjaxResult res = new AjaxResult { code = 200, msg = msg, data = data }; return res; } /// /// 返回错误 /// /// 错误提示 /// protected AjaxResult Error(string msg) { AjaxResult res = new AjaxResult { code = 0, msg = msg, data = null }; return res; } /// /// 返回错误 /// /// 错误提示 /// protected AjaxResult Error(string msg) { AjaxResult res = new AjaxResult { code = 0, msg = msg, data = default }; return res; } } }