In this article I will explain with an example, how to disable mouse right click using JavaScript in ASP.Net Core Razor pages.
Note: For more details on ASP.Net Core Razor Pages, please refer my article ASP.Net Core Razor Pages: Hello World Tutorial with Sample Program example.
 
 
JavaScript Script to disable Right Click
Inside the oncontextmenu event handler, the event is cancelled by returning FALSE.
<script type="text/javascript">
    //Disable the Context Menu event.
    document.oncontextmenu = function () {
        return false;
    };
</script>
 
 
Razor PageModel (Code-Behind)
The PageModel consists of following Handler method.
Handler method for handling GET operation
This Handler method is left empty as it is not required.
public class IndexModel : PageModel
{
    public void OnGet()
    {
    }
}
 
 
Disabling Mouse Right Click using JavaScript in Razor Page (HTML)
You will need to place the following script in HEAD section of your Razor Page (.cshtml) or Layout page.
@page
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script type="text/javascript">
        //Disable the Context Menu event.
        document.oncontextmenu = function () {
            return false;
        };
    </script>
</head>
<body>
    <h1>Try to Right Click on this Page.</h1>
</body>
</html>
 
 
Screenshot
ASP.Net Core Razor Pages: Disable Mouse Right Click using JavaScript
 
 
Browser Compatibility
The above code has been tested in the following browsers.
Microsoft Edge  FireFox  Chrome  Safari  Opera
* All browser logos displayed above are property of their respective owners.
 
 
Demo
 
 
Downloads