- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the significance of NonActionAttribute in ASP .Net MVC C#?
The NonAction attribute is used when we want a public method in a controller but do not want to treat it as an action method. An action method is a public method in a controller that can be invoked using a URL. So, by default, if we have any public method in a controller then it can be invoked using a URL request. To restrict access to public methods in a controller, NonAction attribute can be used.
Now let us consider HomeController having two public methods MyMethod1 and MyMethod2.
Controller
Example
using System.Web.Mvc; namespace DemoMvcApplication.Controllers{ public class HomeController : Controller{ public string MyMethod1(){ return "<h1>My Method 1 Invoked</h1>"; } public string MyMethod2(){ return "<h1>My Method 2 Invoked</h1>"; } } }
Let us invoke both the methods in the HomeController using the below URLs.
http://localhost:59146/Home/MyMethod1
http://localhost:59146/Home/MyMethod2
Let's say MyMethod2 is for some internal purpose, and we don't want it to be invoked using a URL request. To achieve this, we have to decorate it with NonAction attribute.
Controller
Example
using System.Web.Mvc; namespace DemoMvcApplication.Controllers{ public class HomeController : Controller{ public string MyMethod1(){ return "<h1>My Method 1 Invoked</h1>"; } [NonAction] public string MyMethod2(){ return "<h1>My Method 2 Invoked</h1>"; } } }
Below is the output of making a MyMethod2 Non Action.
Output
- Related Articles
- What is ViewData in ASP .Net MVC C#?
- What is the use of ChildActionOnly attribute in ASP .Net MVC C#?
- How to use ViewBag in ASP .Net MVC C#?
- What are the three segments of the default route, that is present in ASP .Net MVC C#?
- What are the levels at which filters can be applied in ASP .Net MVC C#?
- How can we provide an alias name for an action method in Asp .Net MVC C#?
- What is serialization in C#.NET?
- What is the significance of ‘^’ in PHP?
- What is the formula of net displacement?
- what is the significance of finally in javascript?
- What is the significance of Charminar in Hyderabad?
- What is the significance of Hampi Monuments?
- What is the significance of Ajanta Caves?
- What is the significance of Bodh Gaya?
- What is the significance of Taj Mahal?
