控制器存储数据 -->视图层使用数据
开发工具与关键技术:VS、
作者:蒋紫晨
撰写时间:2019-01-19
首先我们从视图层传过一个参数(Parameter)在控制中查询出来自己想要的数据,用Utils类的方法存储
/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
public static void WriteCookie(string strName, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];//发出请求
if (cookie == null)//如果不存在的话就新增一个
{
cookie = new HttpCookie(strName);
}
cookie.Value = HttpContext.Current.Server.UrlEncode(strValue);//赋值
cookie.Expires = DateTime.Now.AddDays(7);//设置存活时间
HttpContext.Current.Response.AppendCookie(cookie);
}
/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
public static void WriteCookie(string strName, string key, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];//发出请求
if (cookie == null)//如果不存在的话就新增一个
{
cookie = new HttpCookie(strName);
}
cookie[key] = HttpContext.Current.Server.UrlEncode(strValue);//赋值
cookie.Expires = DateTime.Now.AddDays(7);//设置存活时间
HttpContext.Current.Response.AppendCookie(cookie);
}
/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
public static void WriteCookie(string strName, string key, string strValue, int expires)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];//发出请求
if (cookie == null)//如果不存在的话就新增一个
{
cookie = new HttpCookie(strName);
}
cookie[key] = HttpContext.Current.Server.UrlEncode(strValue);//赋值
cookie.Expires = DateTime.Now.AddDays(expires);//设置存活时间
HttpContext.Current.Response.AppendCookie(cookie);
}
/// <summary>
/// 写cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="strValue">值</param>
/// <param name="strValue">过期时间(分钟)</param>
public static void WriteCookie(string strName, string strValue, int expires)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];//发出请求
if (cookie == null)//如果不存在的话就新增一个
{
cookie = new HttpCookie(strName);
}
cookie.Value = HttpContext.Current.Server.UrlEncode(strValue);//赋值
cookie.Expires = DateTime.Now.AddMinutes(expires);//设置存活时间
HttpContext.Current.Response.AppendCookie(cookie);
}
用ViewData的方式来存储数据的。
以及输出的截图:
在截图中我们看到,如果我需要在另一个控制器中读取数据内容,见截图
string UserID = Utils.GetCookie("Test", "UserID");//获取用ID
/// <summary>
/// 读cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <returns>cookie值</returns>
public static string GetCookie(string strName)
{
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
{
return HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies[strName].Value.ToString());
}
else
{
return "";
}
}
/// <summary>
/// 读cookie值
/// </summary>
/// <param name="strName">名称</param>
/// <param name="key">key</param>
/// <returns></returns>
public static string GetCookie(string strName, string key)
{
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
{
return HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.Cookies[strName][key].ToString());
}
else
{
return "";
}
}
通过控制器转到视图就可以获取ViewData,见如上截图中获取的ViewData内容,下面的就很简单,我们在下一个接口用例中带上ViewData,见实现的代码结果:
谢谢大家的支持。我会努力加油 。