1,配置web.cofig配置文件修改执行javascript
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
2,编写Autofc.cs并且此文件放在App_Start文件目录下,在应用初始化是进行加载。
<pre code_snippet_id="272713" snippet_file_name="blog_20140403_1_3301351" name="code" class="csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Autofac;
using AutofcDemo.Controllers;
using AutofcDemo.Models;
namespace AutofcDemo.App_Start
{
public static class Autofc
{
public static IContainer container;
public static void InitAutofc()
{
ContainerBuilder builder = new ContainerBuilder();
//----------------------------------------------------------------------------------------
// 构造方法进行自动注入使用方式(官方推荐)
//----------------------------------------------------------------------------------------
//方式一
//builder.RegisterType<ShowDao>();
// builder.RegisterType<HomeController>();
//方式二
//builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(t=>t.Name.EndsWith("Dao")).SingleInstance();
//builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(t => t.Name.EndsWith("Controller")).InstancePerLifetimeScope();
//方式三
// builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());
//----------------------------------------------------------------------------------------
// 属性自动注入使用方式
//----------------------------------------------------------------------------------------
//方式一
//builder.Register(c => new HomeController { showDao = c.Resolve<ShowDao>() });
//方式二
//builder.RegisterType<ShowDao>();
// builder.RegisterType<HomeController>().PropertiesAutowired();
//方式三
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(t => t.Name.EndsWith("Dao")).SingleInstance();
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(t => t.Name.EndsWith("Controller")).InstancePerLifetimeScope().PropertiesAutowired();
//----------------------------------------------------------------------------------------
container = builder.Build();
DependencyResolver.SetResolver(new Autofac.Integration.Mvc.AutofacDependencyResolver(container));
}
}
}
3,在global.cs中进行配置
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using AutofcDemo.App_Start;
using Autofac;
using AutofcDemo.Models;
using AutofcDemo.Controllers;
namespace AutofcDemo
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
Autofc.InitAutofc();
}
}
}
4,在控制器中注入
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutofcDemo.Models;
namespace AutofcDemo.Controllers
{
public class HomeController : Controller
{
//通过属性注入
public ShowDao showDao { set; get; }
//通过构造方法注入
//public HomeController(ShowDao _showDao)
//{
// this.showDao = _showDao;
//}
public ActionResult Index()
{
string aaa = showDao.PrintInfo();
ViewBag.aaa = aaa;
return View();
}
}
}