In this article I will explain with an example, how to disable mouse right click using
JavaScript in ASP.Net MVC.
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>
Controller
The Controller consists of following Action method.
Action method for handling GET operation
Inside this Action method, simple the View is returned.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
}
Disabling Mouse Right Click using JavaScript in View
You will need to place the following script in HEAD section of your View (.cshtml) or Layout 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
Browser Compatibility
* All browser logos displayed above are property of their respective owners.
Demo
Downloads