项目结构图:
App_start文件夹中的文件是VS自己创建的,其中NinjectWebCommon类在创建之初并不存在。后面会再次提到!
添加一个Home控制器。代码如下:
using EssentialTools.Models; using Ninject; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace EssentialTools.Controllers { public class HomeController : Controller { private IValueCalculator calc; Product[] products ={ new Product{Name="Kayak",Category="Watersports",Price=275M}, new Product{Name="LifeJacket",Category="Watersports",Price=48.95M}, new Product{Name="Soccer Ball",Category="Soccer",Price=19.50M}, new Product{Name="Corner Flag",Category="Soccer",Price=34.95M} }; public HomeController(IValueCalculator calcParam) { calc = calcParam; } public ActionResult Index() { //IKernel ninjectKernel = new StandardKernel(); //ninjectKernel.Bind<IValueCalculator>().To<LinqValueCalculator>(); //LinqValueCalculator calc = new LinqValueCalculator(); //return View(calc.ValueProducts(products)); ShoppingCart cart = new ShoppingCart(calc) { Products = products }; decimal totalValue = cart.CalculateProductTotal(); return View(totalValue); } } }