开发者

What is the order of execution in ASP.NET MVC Controller?

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?


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜