Is there a way to execute function before unloading razor view of _Layout asp.net core

I know that I can bind handler to onbeforeunload event and execute something before full page unload. But is there a way to do this on razor view of _Layout?

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum