views:

111

answers:

2

Is it possible for a ASPX view (in ASP.NET MVC) to have non-default constructor AND use this constructor when creating this view?

Example - Page will inherit from this class:

public class ViewPageWithHelper<TModel> : System.Web.Mvc.ViewPage<TModel> where TModel : class
    {
        public ViewPageWithHelper(Helpers helpers)
        {
            Helpers = helpers;
        }

        protected Helpers Helpers { get; private set; }
    }

ASPX view:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="MyInjectedViewPage<MyModel>" %>

<% Helpers.XXXX %>

Now, I'd like to inject Helpers into view somehow - automatically. Ideas?

A: 

I think you should try creating your own IViewEngine that would create the view using default webform view engine and then inject dependencies using property injection.

maciejkow
A: 

Why do you need to inject helpers?

When I've needed helpers in the past I've either written some extension methods for the HtmlHelper or just created a static helper class and used it directly in the view.

jbenckert
Because helpers use some of the injected objects, such as session object, services, repositories and such. Stuff I don't want view to know about...Otherwise, static extension methods are good enough. But I'd like helpers to be unit testable.
bh213