ASP.NET MVC2 - Custom Properties on ViewPage

What I thought should be pretty simple turned out to be a lot more.

Atm I use the base class (MasterModel) for all my models, which are then passed from ViewPage <HomeIndexModel> to ViewMasterPage <MasterModel> and everything works fine. This was done this way after reading the Scott Gu post.

Then I thought about inheriting from ViewPage and extending Factory, or where ViewPage was ever created ... but then I got lost.

Who is responsible for instantiating ViewPage, ViewMasterPage, and ViewUserControl.

The problem is more or less that I use an instance of one class on 99% of all pages and always on MasterPage, so its pain constantly transfers it to the model all the time.

Is this even the right way, or do you have other suggestions?

Update

I need to be able to inject complex types taken from my IOC (StructureMap) into the ViewPage constructor, so it will be easy to change the implementation. That is why I am looking for a place where ViewPage will be created.

+3
source share
3 answers

Can i use MVC3? Inclusion of dependencies in viewing pages is impossible, because the creation is deeply immersed in the implementation of the viewing mechanism.

This is now described in MVC3. Full description: http://bradwilson.typepad.com/blog/2010/07/service-location-pt3-views.html

+2
source

, , , .

:

public sealed class MyViewPage : System.Web.Mvc.ViewPage
{
  public string Herp {get{return "Derp";}}
}

, , :

<%@ Page Language="C#" Inherits="MyApplication.MyViewPage" %>
<!-- compare the Inherits attribute in your standard View page -->
<%: Herp %>

, 90% , . :

public sealed class MyViewPage<T> : System.Web.Mvc.ViewPage<T> {}
0

DI ViewPage, ( ViewData []) , , . , (, , , ..). , , , , . , ViewData. :

<% var headerModel = ViewData[Constants.HeaderData] as HeaderViewModel %>

Or a simple solution: do you think that you use it only ObjectFactoryin your opinion? (It would make some people get into a cringe, but it would work.)

ObjectFactory.GetInstance<IService>();
0
source

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


All Articles