For actions that need to run on each request, we have always put these in the Index() method of the DefaultControllerType and then hooked that controller up in the Umbraco Rendering Defaults Options (9) or using a composer (8).
These on-each-request action are things like
Initializing a cookie that is used during execution
Redirecting based on circumstances (switch language, go directly to "my pages", ...)
Enriching the request pipeline with something based on the requesting user
I'm wondering if there's a better way to inject these on-each-request actions in Umbraco 9 or using ASP.NET Core.
The problem with the Default Controller approach, even if it works, is that
The DefaultControllerTypeIndex() action becomes bloated
The DefaultControllerTypeIndex() must be aware of all on-each-request actions
Any "filtering" to decide whether an on-each-request action is even applicable must be done as part of the action (i.e. an action that should only run for signed-in members)
If we don't use the default controller, all actions are gone
I'm guessing there are Middleware-type ways of doing this in a different way, but I'm mostly interested in Umbraco-specific solutions, since all on-each-request actions will likely involve Umbraco.
Better place than DefaultControllerType Index() to perform on-each-request actions in Umbraco 9 or ASP.NET Core
For actions that need to run on each request, we have always put these in the
Index()
method of theDefaultControllerType
and then hooked that controller up in the Umbraco Rendering Defaults Options (9) or using a composer (8).These on-each-request action are things like
I'm wondering if there's a better way to inject these on-each-request actions in Umbraco 9 or using ASP.NET Core.
The problem with the Default Controller approach, even if it works, is that
DefaultControllerType
Index()
action becomes bloatedDefaultControllerType
Index()
must be aware of all on-each-request actionsI'm guessing there are Middleware-type ways of doing this in a different way, but I'm mostly interested in Umbraco-specific solutions, since all on-each-request actions will likely involve Umbraco.
Thankful for any ideas.
You should still be able to use Umbraco code within custom middlewares. I also use it for a package to track 404 requests: https://github.com/patrickdemooij9/InboundLinkErrors/blob/v9/main/source/InboundLinkErrors.Core/MiddleWare/LinkErrorsMiddleware.cs
And if I understand correctly, you use a User Composer to add this middleware to the Umbraco pipeline:
Should I do the same even though not building a package?
Yes, but in my example I load it after all the Umbraco middleware has finished. I am not quite sure when you want it loaded though.
is working on a reply...