In large apps and sites, you need to do apply global actions e.g. for each button click on a specific page, section or even on all pages in the app. Additionally, you may want to do global exception handling and logging, or switch the culture based on a value from cookies etc.
In DotVVM, we have a concept of filters. If you know Action filters in ASP.NET MVC, it is the same thing.
If you want to apply a common logic to one or more viewmodels, viewmodel commands or a whole application, you need to create a class that derives from
You can then override any of the method listed below.
Presenter-level events (applicable to DotVVM pages and custom presenters)
OnPresenterExecutingAsyncis executed immediately after the URL is mapped to a specific route and presenter is resolved. This method is called also for DotVVM pages as they are handled by
OnPresenterExecutedAsyncis executed immediately after the presenter completes processing of the request. This method is called also for DotVVM pages as they are handled by
OnPresenterExceptionAsyncis executed when an unhandled exception is thrown from the presenter. This method is called also for DotVVM pages as they are handled by
Page-level events (applicable to DotVVM pages)
OnPageInitializedAsyncis executed after the page control tree is built and viewmodel instance is initialized.
OnPageRenderedAsyncis executed after the response is rendered completely and before the viewmodel instance is disposed.
OnPageExceptionAsyncis executed when an unhandled exception occurs during the processing of the DotVVM page.
ViewModel-level events (applicable to DotVVM pages)
OnViewModelCreatedAsyncis executed after the viewmodel instance is created and assigned to the root of the control tree, and the
PreInitphase is completed.
OnViewModelSerializingAsyncis executed after the
PreRenderCompletephase is completed and before the viewmodel is serialized to JSON.
OnViewModelDeserializedAsyncis executed on postbacks, after the viewmodel from the client was deserialized, before the
Loadphase is initiated.
Command-level events (applicable to postbacks on DotVVM pages)
OnCommandExecutingAsyncis executed on postbacks, before the command referenced from a command binding is called.
OnCommandExecutedAsyncis executed on postbacks, after the command referenced from a command binding is called.
There is also a class called
ExceptionFilterAttribute which adds another event:
OnCommandExceptionis executed on postbacks, when the command referenced from a command binding throws an exception.
If you only need to target specific events, you don't need to inherit from these attributes. You can implement the
IViewModelActionFilter interface instead.