Most challenging task in today's application is to secure them from many types of attacks and as developer our job is to provide full security as we have possible in various ways. In this article we will learn how to disable mouse right click using different ways .So considering the preceding requirement I have decided to write this article .Now let's learn step by step, which helps beginners to learn how to disable mouse right click on entire ASP.NET MVC application.
Create an ASP.NET MVC Application.
Now let us start with a step by step approach from the creation of a simple MVC application
I hope you have created ASP.NET MVC Application, there are many ways to disable mouse right click and each one of them has pros and cons , the following are commonly used approaches.
To use it , just called the contextmenu function in page body tag or master page body tag to disable the mouse right click as
Example
Example
Call preceding function inside the body tag of layout page if you wants to disable mouse right click on entire ASP.NET MVC application.
Now open the layout page of our created application and add preceding function inside the layout body tag .after modifying the code the layout page source code will be look like as shown in following example.
_Layout.cshtml
Now create on empty view named Index to check the whether mouse right click disable or not .
Index.cshtml
Now we have done with the code .After running the application in the browser it shows the following output
Create an ASP.NET MVC Application.
Now let us start with a step by step approach from the creation of a simple MVC application
- "Start", then "All Programs" and select "Microsoft Visual Studio 2015".
- "File", then "New" and click "Project", then select "ASP.NET Web Application Template", then provide the Project a name as you wish and click OK. After clicking then choose the empty application and check on MVC option .
I hope you have created ASP.NET MVC Application, there are many ways to disable mouse right click and each one of them has pros and cons , the following are commonly used approaches.
- Using contextmenu in Body tag
- Using contextmenu with jQuery.
- Using JavaScript .
Using oncontextmenu in Body tag
This is the shortest way to disable mouse right click without writing any script code but by default it uses javascript .To use it , just called the contextmenu function in page body tag or master page body tag to disable the mouse right click as
Example
In the preceding example , the function is called in layout body tag hence it will disable mouse right click on entire ASP.NET application, If you wants to disable only page specific then you can call same function in specific page.
Disadvantage of this approach is it wont be work outside the body tag , if click on outside of text then it will allows to mouse right click .Using contextmenu with jQuery
This is the best way to disable the mouse right click which supports on every browser and won't be allow for mouse right click.Example
<script> $(document).ready(function () { $(this).bind("contextmenu", function (e) { e.preventDefault(); }); }); </script>
Now open the layout page of our created application and add preceding function inside the layout body tag .after modifying the code the layout page source code will be look like as shown in following example.
_Layout.cshtml
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>@ViewBag.Title - www.compilemode.com</title> <link href="~/Content/Site.css" rel="stylesheet" type="text/css" /> <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" /> <script src="~/Scripts/jquery-3.1.0.min.js"></script> <script src="~/Scripts/bootstrap.min.js"></script> </head> <body> <script> $(document).ready(function () { $(this).bind("contextmenu", function (e) { e.preventDefault(); }); }); </script> <div class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> @Html.ActionLink("www.compilemode.com", "www.compilemode.com", "Home", new { area = "" }, new { @class = "navbar-brand" }) </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> </ul> </div> </div> </div> <div class="container body-content"> @RenderBody() <hr /> <footer> <p>© @DateTime.Now.Year -www.compilemode.com</p> </footer> </div> </body> </html>
Now create on empty view named Index to check the whether mouse right click disable or not .
Index.cshtml
@{ ViewBag.Title = "www.compilemode.com"; } <hr /> Visit <a href="https://www.blogger.com/null">www.compilemode.com</a> to learn more about Microsoft Technologies. <hr />
Without alert Box
Right click on page , it will not show you mouse right click , it means it disable for page level as wel as on entire ASP.NET MVC application.
With Alert Box
If you wants to show alert Box then just call alert function in our created jQuery function after e.preventDefault() function .
Note:
- Its not good practice to show user mouse right click alert box because of this we exposing our security measurement what we had done so its always good to do not show alert box.
- To use jQuery we need to add the reference for the jQuery library .You can download the latest jQuery library from JQuery official site
From preceding all example we have learned how to to disable mouse right click on page specific as well as on entire application .
Summary
I hope this article is useful for all readers, If you have any suggestion related to article then please contact me.
Related articles
Post a Comment