'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?



Solution 1:[1]

Yes,it could,If you add the js codes in _layout,when you leave the page,it would work I added the codes in my layout:

<script type="text/javascript">
        window.onbeforeunload = function (){
            return("");
        }
    </script>

when I leave the page, a dialogue pop out enter image description here

You could share your codes if you have problems when you add the function of onbeforeunload

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ruikai Feng