How to provide ASP.NET MVC2 main pages with an independent controller model

I use strongly typed views and autofac for Injection Dependency under ASP.NET MVC2, and I'm trying to get a common dynamic header through dependency injection. That is, I want this to happen without the presentation being aloof from this content, even the existing one, and I was hoping to avoid static container detection and manual permission, but I can’t find a way to easily insert the master or partial view into the master mode via ctor or injection properties.

I can’t imagine that this is an unusual task, but all I can find in terms of methods is a subclass of Controller, which stores data in untyped ViewData, subclasses ViewModels to fill in the main data in the model or static resolution, all of which I would prefer not to use. What am I missing?

EDIT : As stated, the DI on the master page is fighting the wireframe. Therefore, my question is poorly framed: I really do not care about DI on the master pages, but I have a dynamic element in the chrome of the site, i.e. On the home page. Providing them with a model should not depend on each controller that uses this wizard, since it is a request context and not specific to a particular controller. I fully admit that injecting directly onto the master pages is inappropriate. If I could register a separate master controller, which will be called additionally, it will be even better. Is it possible? Given this task of providing a master model independent of the controller, what is the appropriate structure approach? Or does shared content in MVC require every controller to know about this content?

+3
source share
2 answers

You can use child actions .

Controller:

public class MyHeaderController: Controller { private readony IRepository _repository; public MyHeaderController(IRepository repository) { _repository = repository; } [ChildActionOnly] public ActionResult Index() { var model = _repository.GetSomeModel(); return PartialView(model); } } 

And somewhere on your main page:

 <div><%= Html.Action("Index", "MyHeader") %></div> 
+7
source

Your problems are confusing the term Injection Dependency and struggling with how the ASP.NET MVC environment works.

Also, you are using the term “Dependency Injection” in the wrong context. You are trying to use a hammer as a chisel.

MasterPages and views in ASP.NET MVC are designed to be used as templates. As indicated in another answer, childish activities solve your problem.

For future reference:

Dependency Injection is a tool for setting parameters that you need to enter in class constructors and do it automatically for you, overriding some of the default values ​​for frameworks. The purpose of this is to decouple the components so that they become more reusable, more verifiable, more unitary, among other good things.

DI refers to a problem with the code and solves the problem, not the problem with the user interface.

What you are trying to do is simply impossible. That is, to enter “dependencies” through the designers and properties into the main page. Again, MasterPages are designed by ASP.NET MVC for use as templates. They do not have the code behind the class to create an instance through the constructor, which allows you to enter dependencies in it.

In other words, you are fighting with the frame, which means that you do not understand this.

If this sounds like nitpicking, I think it needs to be highlighted, because otherwise you will confuse yourself and others who read this stream in the future.

0
source

Source: https://habr.com/ru/post/1401849/


All Articles