标签:book ice property abs wpa ide nbsp erb alt
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTable.Routes); var builder = new ContainerBuilder(); builder.RegisterType<BookService>().As<IBookService>(); builder.RegisterType<Logger>().As<ILogger>(); // Register your MVC controllers. (MvcApplication is the name of // the class in Global.asax.) builder.RegisterControllers(typeof(MvcApplication).Assembly); // OPTIONAL: Register model binders that require DI. builder.RegisterModelBinders(typeof(MvcApplication).Assembly); builder.RegisterModelBinderProvider(); // OPTIONAL: Register web abstractions like HttpContextBase. builder.RegisterModule<AutofacWebTypesModule>(); // OPTIONAL: Enable property injection in view pages. builder.RegisterSource(new ViewRegistrationSource()); // OPTIONAL: Enable property injection into action filters. builder.RegisterFilterProvider(); // Set the dependency resolver to be Autofac. var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); }
builder.RegisterType<HomeController>().InstancePerRequest();
[ModelBinderType(typeof(Book))] public class BookModelBinder : IModelBinder { public ILogger logger; public BookModelBinder(ILogger logger) { this.logger = logger; } public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { HttpRequestBase request = controllerContext.HttpContext.Request; string title = request.Form.Get("Title"); string BookID = request.Form.Get("BookID"); string day = request.Form.Get("Day"); string month = request.Form.Get("Month"); string year = request.Form.Get("Year"); return new Book { BookID=BookID, Title=title+":DI Test"+this.logger.Log("dsa")+request.Form.Get("HttpRequestBaseDI"), Date=year+"-"+month+"-"+day }; } }
// OPTIONAL: Register web abstractions like HttpContextBase.
builder.RegisterModule<AutofacWebTypesModule>();
[ModelBinderType(typeof(Book))] public class BookModelBinder : IModelBinder { ILogger logger; HttpRequestBase request1; public BookModelBinder(ILogger logger, HttpRequestBase request) { this.logger = logger; this.request1 = request; } public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { HttpRequestBase request = controllerContext.HttpContext.Request; request = request1; string title = request.Form.Get("Title"); string BookID = request.Form.Get("BookID"); string day = request.Form.Get("Day"); string month = request.Form.Get("Month"); string year = request.Form.Get("Year"); return new Book { BookID=BookID, Title=title+":DI Test"+this.logger.Log("dsa")+request.Form.Get("HttpRequestBaseDI"), Date=year+"-"+month+"-"+day }; } }
builder.RegisterSource(new ViewRegistrationSource());
这里的例子使用的是一个强类型的View,所以实现了一个泛型ViewPage
public abstract class CustomViewPage<T> : WebViewPage<T>@inherits MVC5Practices.Infrastructure.CustomViewPage<MVC5Practices.Infrastructure.Book> @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>ShowBook</title> </head> <body> <div> <h4>Book</h4> <h5>@Logger.Log("dsa")</h5> <hr /> <dl class="dl-horizontal"> <dt> @Html.DisplayNameFor(model => model.Title) </dt> <dd> @Html.DisplayFor(model => model.Title) </dd> <dt> @Html.DisplayNameFor(model => model.Date) </dt> <dd> @Html.DisplayFor(model => model.Date) </dd> </dl> </div> <p> @Html.ActionLink("Edit", "Edit", new { id = Model.BookID }) | @Html.ActionLink("Back to List", "Index") </p> </body> </html>
// OPTIONAL: Enable property injection into action filters.
builder.RegisterFilterProvider();
public class CustomActionFilter : ActionFilterAttribute { public ILogger Logger { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { Logger.Log("OnActionExecuting"); } }
标签:book ice property abs wpa ide nbsp erb alt
原文地址:http://www.cnblogs.com/LittleFeiHu/p/6155883.html