1、将关键字加入cookie缓存中
///
/// 添加搜索关键字
///
///
private void addKeyWord(string keyWord)
{
keyWord = keyWord.Trim();
HttpCookie cook;
if (Request.Cookies["keyWordList"] == null)
{
cook = new HttpCookie("keyWordList");
cook.Expires = DateTime.Now.AddMonths(1);
}
else
{
cook = Request.Cookies["keyWordList"];
}
List list = getKeyWord(HttpContext);
if (list.Contains(keyWord))
{
list.RemoveAll(p => p == keyWord);
}
if (list.Count >= 3)
{
list.RemoveAt(0);
}
list.Add(keyWord);
string productList = JsonConvert.SerializeObject(list);
cook.Value = HttpUtility.UrlEncode(productList, Encoding.UTF8);
cook.Expires = DateTime.Now.AddDays(5);
Response.Cookies.Add(cook);
}
2、获取搜索关键字
///
/// 获取搜索关键字
///
///
public List getKeyWord(HttpContextBase context)
{
HttpCookie cook = context.Request.Cookies["keyWordList"];
List result = new List();
if (cook == null)
{
return result;
}
else
{
JsonSerializer jserial = new JsonSerializer();
StringReader strread = new StringReader(HttpUtility.UrlDecode(cook.Value, Encoding.UTF8));
result = jserial.Deserialize
- >(new JsonTextReader(strread));
}
return result;
}
3、在页面上渲染 调用