| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 | 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{    /// <summary>    ///     /// </summary>    [ApiController]    [Route("api/[controller]/[action]")]    public class BaseController : ControllerBase    {        private readonly string uploadFolder;        /// <summary>        ///         /// </summary>        public BaseController()        {            this.uploadFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot");        }        /// <summary>        /// 确保Controller程序集被加载        /// </summary>        [NonAction]        public void EnsureAssemblyLoaded()        {         }        /// <summary>        /// 返回本地文件        /// </summary>        /// <param name="fileName"></param>        /// <returns></returns>        protected string GetLocalFile(string fileName)        {            return Path.Combine(this.uploadFolder, fileName);        }        /// <summary>        /// 获取客户端IP地址        /// </summary>        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;            }        }        /// <summary>        /// 获取客户端使用的连接端口        /// </summary>        protected int RemotePort        {            get            {                return HttpContext.Connection.RemotePort;            }        }        /// <summary>        /// 获取本地IP地址        /// </summary>        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;            }        }        /// <summary>        /// 获取本地使用的http端口        /// </summary>        protected int LocalPort        {            get            {                return HttpContext.Connection.LocalPort;            }        }        /// <summary>        /// 返回成功        /// </summary>        /// <param name="data">返回的数据</param>        /// <returns></returns>        protected AjaxResult Success(object data = null)        {            AjaxResult res = new AjaxResult            {                code = 200,                msg = "请求成功!",                data = data            };            return res;        }        /// <summary>        /// 返回成功        /// </summary>        /// <param name="msg">返回的消息</param>        /// <param name="data">返回的数据</param>        /// <returns></returns>        protected AjaxResult Success(object data, string msg = "请求成功!")        {            AjaxResult res = new AjaxResult            {                code = 200,                msg = msg,                data = data            };            return res;        }        /// <summary>        /// 返回成功        /// </summary>        /// <param name="msg">返回的消息</param>        /// <param name="data">返回的数据</param>        /// <returns></returns>        protected AjaxResult<T> Success<T>(T data, string msg = "请求成功!")        {            AjaxResult<T> res = new AjaxResult<T>            {                code = 200,                msg = msg,                data = data            };            return res;        }        /// <summary>        /// 返回错误        /// </summary>        /// <param name="msg">错误提示</param>        /// <returns></returns>        protected AjaxResult Error(string msg)        {            AjaxResult res = new AjaxResult            {                code = 0,                msg = msg,                data = null            };            return res;        }        /// <summary>        /// 返回错误        /// </summary>        /// <param name="msg">错误提示</param>        /// <returns></returns>        protected AjaxResult<T> Error<T>(string msg)        {            AjaxResult<T> res = new AjaxResult<T>            {                code = 0,                msg = msg,                data = default            };            return res;        }    }}
 |