Home >Backend Development >C#.Net Tutorial >What is the meaning of NonActionAttribute in ASP .Net MVC C#?
When we want to use a public method in the controller but don't want to treat it as an action method, we can use the NonAction attribute. Action methods are public methods in the controller that can be called using a URL. So, by default, if there is any public method in a controller, it can be called using a URL request. To restrict access to public methods in a controller, you can use the NonAction attribute.
Now let us consider that HomeController has two public methods MyMethod1 and MyMethod2.
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 call these two methods in HomeController using the following URL.
http://localhost:59146/Home/MyMethod1
##http://localhost:59146/Home/MyMethod2
Let's say MyMethod2 is used for some internal purpose and we don't want to call it Use URL requests. To achieve this we have to decorate it with NonAction Attributes. ControllerExampleusing 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>"; } } }
The above is the detailed content of What is the meaning of NonActionAttribute in ASP .Net MVC C#?. For more information, please follow other related articles on the PHP Chinese website!