using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Helpers
{
//.net Framework MVCHelper
//public class MVCHelper
//{
// private HttpContextBase _HttpContext { get; set; }
// private HttpRequestBase _HttpRequest { get; set; }
// private HttpResponseBase _HttpResponse { get; set; }
// private MVCHelper() { }
// public MVCHelper(HttpContextBase httpContext) {
// _HttpContext = httpContext;
// _HttpRequest = _HttpContext.Request;
// _HttpResponse = _HttpContext.Response;
// }
// ///
// /// 设置本地cookie
// ///
// /// 键
// /// 值
// /// 过期时长,单位:分钟
// public void SetCookies(string key, string value, int minutes = 300,string path = "/",bool isDomain = false)
// {
// HttpCookie cookie = new HttpCookie(key, value);
// if (isDomain)
// {
// string host = Helpers.Tool.GetAppSetting("hostName");
// cookie.Domain = host;
// }
// cookie.Path = path;
// cookie.Expires = DateTime.Now.AddMinutes(minutes);
// _HttpResponse.Cookies.Add(cookie);
// }
// ///
// /// 设置只读cookie
// ///
// /// 键
// /// 值
// /// 过期时长,单位:分钟
// public void SetOnlyCookies(string key, string value, int minutes = 300, string path = "/", bool isDomain = false)
// {
// HttpCookie cookie = new HttpCookie(key, value);
// if (isDomain)
// {
// string host = Helpers.Tool.GetAppSetting("hostName");
// cookie.Domain = host;
// }
// cookie.Path = path;
// cookie.HttpOnly = true;
// cookie.Expires = DateTime.Now.AddMinutes(minutes);
// _HttpRequest.Cookies.Add(cookie);
// }
// ///
// /// 删除指定的cookie
// ///
// /// 键
// public void DeleteCookies(string key)
// {
// HttpCookie cookie = _HttpRequest.Cookies[key];
// if (cookie == null) {
// return;
// }
// cookie.Expires = DateTime.Now.AddDays(-1);
// _HttpResponse.AppendCookie(cookie);
// }
// ///
// /// 更新cookie
// ///
// /// 键
// /// 值
// /// 过期时长,单位:分钟
// public void UpdateCookies(string key, string value, int minutes = 300, string path = "/") {
// HttpCookie cookie = _HttpRequest.Cookies[key];
// if (cookie == null)
// {
// return;
// }
// cookie.Path = path;
// cookie.Value = value;
// cookie.Expires = DateTime.Now.AddMinutes(minutes);
// _HttpResponse.AppendCookie(cookie);
// }
// ///
// /// 获取cookies
// ///
// /// 键
// /// 返回对应的值
// public string GetCookies(string key)
// {
// HttpCookie cookie = _HttpRequest.Cookies.Get(key);
// if (cookie == null)
// {
// return null;
// }
// string value = cookie.Value;
// if (string.IsNullOrEmpty(value))
// value = string.Empty;
// return value;
// }
//}
public class CoreMVCHelper
{
private HttpContext _HttpContext { get; set; }
private HttpRequest _HttpRequest { get; set; }
private HttpResponse _HttpResponse { get; set; }
private CoreMVCHelper() { }
public CoreMVCHelper(HttpContext httpContext)
{
_HttpContext = httpContext;
_HttpRequest = _HttpContext.Request;
_HttpResponse = _HttpContext.Response;
}
///
/// 设置本地cookie
///
/// 键
/// 值
/// 过期时长,单位:分钟
public void SetCookies(string key, string value, int minutes = 300, string path = "/", bool isDomain = false)
{
CookieOptions cookieOptions = new CookieOptions();
if (isDomain) {
string host = Helpers.Tool.GetSystemConfig().HostName;
cookieOptions.Domain = host;
}
cookieOptions.Path = path;
cookieOptions.Expires = DateTime.Now.AddMinutes(minutes);
_HttpContext.Response.Cookies.Append(key, value, cookieOptions);
}
///
/// 设置只读cookie
///
/// 键
/// 值
/// 过期时长,单位:分钟
public void SetOnlyCookies(string key, string value, int minutes = 300, string path = "/", bool isDomain = false)
{
CookieOptions cookieOptions = new CookieOptions();
if (isDomain)
{
string host = Helpers.Tool.GetSystemConfig().HostName;
cookieOptions.Domain = host;
}
cookieOptions.Path = path;
cookieOptions.Expires = DateTime.Now.AddMinutes(minutes);
cookieOptions.HttpOnly = true;
_HttpContext.Response.Cookies.Append(key, value, cookieOptions);
}
///
/// 删除指定的cookie
///
/// 键
public void DeleteCookies(string key)
{
_HttpContext.Response.Cookies.Delete(key);
}
///
/// 更新cookie
///
/// 键
/// 值
/// 过期时长,单位:分钟
public void UpdateCookies(string key, string value, int minutes = 300, string path = "/")
{
SetCookies(key, value, minutes, path);
}
///
/// 获取cookies
///
/// 键
/// 返回对应的值
public string GetCookies(string key)
{
string value = string.Empty;
if (_HttpContext.Request.Cookies.TryGetValue(key, out value)) {
return value;
}
else {
return string.Empty;
}
}
}
public class CustomExceptionFilterAttribute : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
if (context.Exception != null)
{
//LogSystemHelper.Error_Log.Write(context.Exception, MethodBase.GetCurrentMethod(), "程序运行异常");
Helpers.Tool.PrintLog_Error("程序运行异常" + context.Exception.Message, context.Exception.StackTrace);
context.HttpContext.Response.StatusCode = 500;
context.Result = new Microsoft.AspNetCore.Mvc.JsonResult(new { status = "error", code = 1, msg = "程序运行异常" });
}
}
}
}