.net core 中对 QueryString,From,Session,Cookie 的封装

 public class HtmlContextSheep
    {
        #region 获取 From 和 Query 的数据

        /// <summary>
        ///获取POST 表单提交的值
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="isSafe">是否进行安全检查</param>
        /// <returns>返回From表单值</returns>
        public static string Form(Microsoft.AspNetCore.Http.HttpContext context, string key, bool isSafe = true)
        {
            string result = "";
            if (!string.IsNullOrWhiteSpace(context.Request.Form[key]))
            {
                result = context.Request.Form[key];
                if (isSafe)
                {
                    if (Common.SafeCommon.IsSafeString(result))
                    {
                        return result;
                    }
                }

            }
            return result;
        }

        /// <summary>
        /// 获取GET 请求的值
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="isSafe">是否进行安全检查</param>
        /// <returns></returns>
        public static string Query(Microsoft.AspNetCore.Http.HttpContext context, string key, bool isSafe = true)
        {
            string result = "";
            if (!string.IsNullOrWhiteSpace(context.Request.Query[key]))
            {
                result = context.Request.Query[key];
                if (isSafe)
                {
                    if (Common.SafeCommon.IsSafeString(result))
                    {
                        return result;
                    }
                }
            }
            return result;
        }

        #endregion

        #region session 的操作

        #region 设置session

        /// <summary>
        /// 设置session
        /// </summary>
        /// <param name="key">session的键</param>
        /// <param name="value">session的值</param>
        /// Microsoft.AspNetCore.Http.HttpContext context,
        public static void SetSession(Microsoft.AspNetCore.Http.HttpContext context,string key, string value)
        {
            if (String.IsNullOrWhiteSpace(context.Session.GetString(key)))
            {
                context.Session.Remove(key);
            }
            context.Session.SetString(key, value);
        }

        #endregion

        #region 获取Session
        /// <summary>
        /// 获取Session
        /// </summary>
        /// <param name="key">session的键</param>
        /// <returns>返回结果的字符串</returns>
        public static String GetSession(Microsoft.AspNetCore.Http.HttpContext context, string key)
        {
            string result = "";
            result = context.Session.GetString(key);

            return result;
        }
        #endregion

        #endregion

        #region cookie的操作


        #region 为cookie赋值       public static void SetCookie(string key, String value , int count=1)
        /// <summary>
        /// 为cookie赋值
        /// </summary>
        /// <param name="key">cookie的名字</param>
        /// <param name="value">值</param>
        /// <param name="count">cookie的有效时间(以天为单位)</param>
        public static void SetCookie(Microsoft.AspNetCore.Http.HttpContext context, string key, String value, int count = 1)
        {
            //value = EnDecrypt.Encrypt(value);
            CookieOptions options = new CookieOptions();
            options.Expires = DateTime.Now.AddDays(count);
            if (context.Request.Cookies.ContainsKey(key))
            {
                options.Expires = DateTime.Now.AddDays(-2);
            }
            context.Response.Cookies.Append(key, value, options);
        }
        #endregion


        #region  获取cookie的值      public static string GetCookie(string cookieName)
        /// <summary>
        /// 获取cookie的值
        /// </summary>
        /// <param name="cookieName">cookie的名字</param>
        /// <returns>返回cookie值</returns>
        public static string GetCookie(Microsoft.AspNetCore.Http.HttpContext context, string cookieName)
        {
            string result = "";
            context.Request.Cookies.TryGetValue(cookieName, out result);
            if (!String.IsNullOrWhiteSpace(result))
            {
                //result = EnDecrypt.Decrypt(result);
            }
            return result;
        }
        #endregion


        #region 移除cookie     public static void DeleteCookie(string cookieName)
        /// <summary>
        /// 移除cookie
        /// </summary>
        /// <param name="cookieName">要移除cookie的名字</param>
        public static void DeleteCookie(Microsoft.AspNetCore.Http.HttpContext context, string cookieName)
        {
            context.Response.Cookies.Delete(cookieName);
        }
        #endregion


        #endregion
    }

 

/* 使用说明
 * 使用Session时
* 在Startup类   ConfigureServices(IServiceCollection services) 方法中
* 注册 session服务 (services.AddSession();)  在Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
* 中添加 session 配置 app.UseSession(); 此配置不要加在 if else 条件判断中
*/

课程通过实际项目融入常用开发技术架构,讲授风格独特,提供详细上课日志及答疑,赠送配套的项目架构源码注释详细清晰且表达通俗,均能直接在实际项目应用,正真的物超所值,价格实惠任务作业:综合运用《C#/.Net企业级系统架构设计实战精讲教程》课程所学知识技能设计一个学生成绩管理系统的架构。要求:1.系统基于MVC的三层架构,各层单独建不同的解决方案文件夹。2.采用Model First开发方式,设计架构时只需要设计学生表(TbStudent)和课程表(TbCourse)。学生表必须有的字段是ID、stuName、age;课程表必须有的字段是ID、courseName、content。3.数据访问层采用Entity Framework或NHibernate来实现,必须封装对上述表的增删改查方法。4.必须依赖接口编程,也就是必须要有数据访问层的接口层、业务逻辑层的接口层等接口层。层层之间必须减少依赖,可以通过简单工厂或抽象工厂。5.至少采用简单工厂、抽象工厂、Spring.Net等技术的2种来减少层与层之间的依赖等。6.封装出DbSession类,让它拥有所有Dal层实例和SaveChanges方法。7.设计出数据访问层及业务逻辑层主要类的T4模板,以便实体增加时自动生成相应的类。8.表现层要设计相关的控制器和视图来验证设计的系统架构代码的正确性,必须含有验证增删改查的方法。9.开发平台一定要是Visual Studio平台,采用C#开发语言,数据库为SQL Server。10.提交整个系统架构的源文件及生成的数据库文件。(注意: 作业需写在CSDN博客,请把作业链接贴在评论区,老师会定期逐个批改~~)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值