首先我在global.ascx中,在应用程序启动之前添加了如下代码,调用了Application_start 进行了依赖注入
namespace MVC3.Extensions
{
public class Application_Start
{
public static void Run()
{
var builder = new ContainerBuilder();
var data = Assembly.Load("Service");
builder.RegisterAssemblyTypes(data)
.Where(a=>a.FullName.Contains("SqlServer")).AsImplementedInterfaces();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
}
在我的项目中有IService,Service,Models这三个类库,但是我今天阅读别人的代码,发现在网站启动的HomeController的构造函数中存在
public class HomeController : Controller
{
private readonly INewsService _newsService;
//
// GET: /Home/
private const string picturePath = "~/xml/pictureRoll.xml";
public ActionResult Index()
{
List<News> list = _newsService.GetAll();
return View(list);
}
public HomeController(INewsService newsService)
{
this._newsService = newsService;
}
public ActionResult PictureRoll()
{
System.Text.StringBuilder picture = new System.Text.StringBuilder();
XElement xe = XElement.Load(Server.MapPath(picturePath));
var query = from value in xe.Elements("img")
select value;
foreach (var item in query)
{
picture.Append(string.Format("<img src=\"/File/ScrollImage/{0}\" title=\"{1}\" alt=\"{1}\"/>",item.Element("path").Value,item.Element("title").Value));
}
ViewBag.PictureScroll = picture.ToString();
return View();
}
}
我现在就想知道,这个构造函数中的参数是如何传递到homeController的
缥缈止盈
GCT1015
大话西游666