views:

99

answers:

1

Say I have a controller called "HomeController" which inherits from Mvc.Controller. Also say I have written the constructor of the controller and some filters for some actions.

Public Class ClientController
    Inherits System.Web.Mvc.Controller

    Public Sub New()
        ''Some code
    End Sub

    <SomeActionFilter()> _
    Function Index() As ActionResult
        Return View()
    End Function
End Class

My questions are :

  1. What is the order of execution of constructor, filter, action?
  2. Can I have a filter for the Constructor, if I do not want to run the code in it by checking for some conditions?
A: 

Constructor will go first. Depending on the type of filter that either the filter or action method will execute second. See here for the filter types.

Filter doesn't intercept constructor. If you want to control the constructor invocation of Controller, you need to build a custom controller factory by implementing IControllerFactory (or DefaultControllerFactory) and register it with ControllerBuidler, e.g.: ControllerBuilder.Current.SetControllerFactory(typeof(MyControllerFactory)) on application start.

if I do not want to run the code in it by checking for some conditions?

Can't you write that code directly in the constructor (e.g. the check condition in the base controller to reuse)? Why do you need a filter in this case?

Buu Nguyen