asp.net mvc 依赖注入Ninject
1.安装Ninject
2.使用Ninject
一 安装Ninject
Nuget:Ninject
二 使用Ninject
public interface IStudent
{
string GetName();
}
public class Student : IStudent
{
public string GetName()
{
return "ligenyun";
}
}
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel; public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
} private void AddBindings()
{
ninjectKernel.Bind<IStudent>().To<Student>();
} protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing; namespace WebApplicationNinject
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
}
}
}
using Ninject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplicationNinject.Models; namespace WebApplicationNinject.Controllers
{
public class StudentController : Controller
{
IStudent calcImpl; public StudentController(IStudent _calcImpl)
{
calcImpl = _calcImpl;
} // GET: Student
public ActionResult Index()
{
ViewData["test"] = calcImpl.GetName();
return View();
}
}
}
@{
ViewBag.Title = "Index";
} <h2>@ViewData["test"]</h2>
asp.net mvc 依赖注入Ninject的更多相关文章
- # ASP.NET Core依赖注入解读&使用Autofac替代实现
标签: 依赖注入 Autofac ASPNETCore ASP.NET Core依赖注入解读&使用Autofac替代实现 1. 前言 2. ASP.NET Core 中的DI方式 3. Aut ...
- ASP.NET MVC IOC 之Ninject攻略
ASP.NET MVC IOC 之Ninject攻略 一.为什么要使用Ninject? 很多其它类型的IOC容器过于依赖配置文件,老是配置,总感觉有点不爽,而且要使用assembly-qualifie ...
- ASP.NET MVC中使用Ninject
ASP.NET MVC中使用Ninject 在[ASP.NET MVC 小牛之路]系列上一篇文章(依赖注入(DI)和Ninject)的末尾提到了在ASP.NET MVC中使用Ninject要做的两件事 ...
- [译]ASP.NET Core依赖注入深入讨论
原文链接:ASP.NET Core Dependency Injection Deep Dive - Joonas W's blog 这篇文章我们来深入探讨ASP.NET Core.MVC Core中 ...
- ASP.NET Core依赖注入解读&使用Autofac替代实现【转载】
ASP.NET Core依赖注入解读&使用Autofac替代实现 1. 前言 2. ASP.NET Core 中的DI方式 3. Autofac实现和自定义实现扩展方法 3.1 安装Autof ...
- 实现BUG自动检测 - ASP.NET Core依赖注入
我个人比较懒,能自动做的事绝不手动做,最近在用ASP.NET Core写一个项目,过程中会积累一些方便的工具类或框架,分享出来欢迎大家点评. 如果以后有时间的话,我打算写一个系列的[实现BUG自动检测 ...
- ASP.NET Core依赖注入——依赖注入最佳实践
在这篇文章中,我们将深入研究.NET Core和ASP.NET Core MVC中的依赖注入,将介绍几乎所有可能的选项,依赖注入是ASP.Net Core的核心,我将分享在ASP.Net Core应用 ...
- 自动化CodeReview - ASP.NET Core依赖注入
自动化CodeReview系列目录 自动化CodeReview - ASP.NET Core依赖注入 自动化CodeReview - ASP.NET Core请求参数验证 我个人比较懒,能自动做的事绝 ...
- ASP.NET Core 依赖注入基本用法
ASP.NET Core 依赖注入 ASP.NET Core从框架层对依赖注入提供支持.也就是说,如果你不了解依赖注入,将很难适应 ASP.NET Core的开发模式.本文将介绍依赖注入的基本概念,并 ...
随机推荐
- Windows 聚焦的锁屏壁纸设置为桌面壁纸
需求: Windows的锁屏壁纸偶尔遇到非常喜欢的壁纸,想设置为桌面壁纸. 步骤如下: 1. “Windows 聚焦”的锁屏壁纸都保存在隐藏文件夹 --- Assets里. a. 打开“资源管理器 b ...
- 【TJOI 2019】唱、跳、rap和篮球
题意 有 $a$ 个 $0$,$b$ 个 $1$,$c$ 个 $2$,$d$ 个 $3$,求有多少种长度为 $n$ 且不包含 $0123$ 这个子串的字符串个数. $n\le 1000,\space ...
- hiho #1485 : hiho字符串(滑动窗口)
#1485 : hiho字符串 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 如果一个字符串恰好包含2个'h'.1个'i'和1个'o',我们就称这个字符串是hiho字符 ...
- {"errcode":40029,"errmsg":"invalid code, hints: [ req_id: LEgfftXIRa-BU8_9 ]"}
{"errcode":40029,"errmsg":"invalid code, hints: [ req_id: LEgfftXIRa-BU8_9 ...
- cdh搭建仓库
搭建内部仓库使用yum安装cm Creating a Permanent Internal Repository 1,安装httpd yum install httpd 删除/etc/httpd/co ...
- C# 获取 与 修改 web.config中的值(修改Xml文件)
定义web.config 中 appSettings 节点 <appSettings> <add key="domainExist" value="fa ...
- 51 Nod 1085 01背包问题
1085 背包问题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2--Wn(Wi为 ...
- 51nod 1120 机器人走方格V3
1120 机器人走方格 V3 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 收藏 关注 N * N的方格,从左上到右下画一条线.一个机器人从左上走到右下,只 ...
- codevs 1405 牛的旅行x
牛的旅行 [问题描述] 农民John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧场不连通.现在,John想在农场里添加一条路径 ...
- Hexo + github pages + 阿里云绑定域名搭建个人博客
申请域名 万网购买的域名,地址:https://wanwang.aliyun.com/domain/com?spm=5176.8142029.388261.137.LoKzy7 控制台进行解析 控制台 ...