Home >Backend Development >C#.Net Tutorial >What is the meaning of NonActionAttribute in ASP .Net MVC C#?

What is the meaning of NonActionAttribute in ASP .Net MVC C#?

王林
王林forward
2023-08-27 20:01:011152browse

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.

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 call these two methods in HomeController using the following URL.

http://localhost:59146/Home/MyMethod1

##http://localhost:59146/Home/MyMethod2

NonActionAttribute 在 ASP .Net MVC C# 中的意义是什么?

NonActionAttribute 在 ASP .Net MVC C# 中的意义是什么?

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.

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>";
      }
   }
}

The following is the output of making MyMethod2 non-operational.

Output

NonActionAttribute 在 ASP .Net MVC C# 中的意义是什么?

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete