BaseController.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.Extensions.Configuration;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.IO;
  9. using Microsoft.AspNetCore.Http;
  10. namespace PMS.NetCore.Controllers
  11. {
  12. public class BaseController : Controller
  13. {
  14. protected NLog.Logger logger;
  15. protected IConfigurationRoot Configuration;
  16. public BaseController()
  17. {
  18. IConfigurationBuilder builder = new ConfigurationBuilder()
  19. .SetBasePath(Directory.GetCurrentDirectory())
  20. .AddJsonFile("appsettings.json");
  21. Configuration = builder.Build();
  22. logger = NLog.LogManager.GetCurrentClassLogger();
  23. }
  24. /// <summary>
  25. /// 设置本地cookie
  26. /// </summary>
  27. /// <param name="key">键</param>
  28. /// <param name="value">值</param>
  29. /// <param name="minutes">过期时长,单位:分钟</param>
  30. protected void SetCookies(string key, string value, int minutes = 600)
  31. {
  32. HttpContext.Response.Cookies.Append(key, value, new CookieOptions
  33. {
  34. Expires = DateTime.Now.AddMinutes(minutes)
  35. });
  36. }
  37. protected void SetCookies(string key, string value)
  38. {
  39. HttpContext.Response.Cookies.Append(key,value, new CookieOptions
  40. {
  41. Expires = DateTime.Now.AddMinutes(600)
  42. });
  43. }
  44. /// <summary>
  45. /// 删除指定的cookie
  46. /// </summary>
  47. /// <param name="key">键</param>
  48. protected void DeleteCookies(string key)
  49. {
  50. HttpContext.Response.Cookies.Delete(key);
  51. }
  52. /// <summary>
  53. /// 获取cookies
  54. /// </summary>
  55. /// <param name="key">键</param>
  56. /// <returns>返回对应的值</returns>
  57. protected string GetCookies(string key)
  58. {
  59. HttpContext.Request.Cookies.TryGetValue(key, out string value);
  60. if (string.IsNullOrEmpty(value))
  61. value = string.Empty;
  62. return value;
  63. }
  64. }
  65. }