Asp.net MVC学习日记十七(页面Cache)

1、Models/Product.cs

public class Product
{
public Guid ProductID { get; set; }
public string Name { get; set; }
public decimal Discount { get; set; }
public decimal Retail { get; set; }
public decimal Tax { get; set; }
public decimal Price
{
get
{
decimal finalCost = Retail;
if (Discount > 0)
finalCost = finalCost - (finalCost * Discount);
finalCost = finalCost + (finalCost * Tax);
return finalCost;
}
private set { }
}
}

2、Models/ProductService.cs

public class ProductService
{
public List<Product> GetProducts()
{
Thread.Sleep(1000); //connect to the database
Thread.Sleep(1000); //run a query and return data
Random rnd = new Random(99);
return Builder<Product>
.CreateListOfSize(1000)
.WhereAll()
.Have(p => p.Discount = Convert.ToDecimal("." +
rnd.Next(1, 99)))
.WhereAll()
.Have(p => p.Tax = new decimal(.10))
.Build()
.ToList();
}
}

3、Controllers/HomeController.cs

[OutputCache(Duration = (60 * 60), VaryByParam = "none")]
public ActionResult Products()
{

Stopwatch sw = new Stopwatch();
sw.Start();
List<Product> products = new ProductService().GetProducts();
sw.Stop();
Response.Write(sw.ElapsedMilliseconds);
return View(products);
}

4、添加Products强类型视图,并且实现List的视图内容

ok

你可能感兴趣的:(asp.net)