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#?

王林
Release: 2023-08-27 20:01:01
forward
1133 people have browsed it

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>";
      }
   }
}
Copy after login

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>";
      }
   }
}
Copy after login
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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template