10
1
using System;
2
using System.ComponentModel.DataAnnotations;
3
4
namespace HelloWorldMvcApp
5
{
6
public class SampleViewModel
7
{
8
9
}
10
}
25
1
@{
2
Layout = null;
3
}
4
5
<!DOCTYPE html>
6
<html lang="en">
7
<head>
8
<meta charset="utf-8">
9
<meta http-equiv="X-UA-Compatible" content="IE=edge">
10
<meta name="viewport" content="width=device-width, initial-scale=1">
11
<title>session id</title>
12
13
14
</head>
15
16
<body>
17
18
<h2>Index</h2>
19
@ViewBag.Cache
20
<hr />
21
SesseionID5
22
@System.Web.HttpContext.Current.Session.SessionID
23
24
</body>
25
</html>
32
1
using System;
2
using System.Web;
3
using System.Web.Caching;
4
using System.Web.Mvc;
5
6
namespace HelloWorldMvcApp
7
{
8
public class HomeController : Controller
9
{
10
public ActionResult Index()
11
{
12
ViewBag.Cache = GetCache("Index");
13
return View();
14
}
15
16
private string GetCache(string key)
17
{
18
string result = "nothing";
19
if (HttpRuntime.Cache.Get(key) == null)
20
{
21
string tmp = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss+08:00");
22
HttpRuntime.Cache.Add(key, tmp, null, DateTime.Now.AddSeconds(10), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
23
result = tmp;
24
}
25
else
26
{
27
result = (string)HttpRuntime.Cache.Get(key);
28
}
29
return result;
30
}
31
}
32
}