项目结构图:

      在MVC项目中使用Ninject

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);
        }
       
    }
}
HomeController.cs

相关文章:

  • 2021-09-15
  • 2022-12-23
  • 2022-12-23
  • 2021-06-27
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
  • 2021-10-23
  • 2022-12-23
相关资源
相似解决方案