What is NonAction Attribute ?

In ASP.NET MVC, every public method of controller is accessible through the url regardless of return type.ย If we create any public method in the controller which is not intend to response as like action method.ย But it also accessible through the browser ULR.ย To overcome this scenario, the NonAction attribute has introduced

“All methods in MVC are treated as a action method. “

Purpose of using :

  • ย As per above statement,ย all public methods of a controller class are basically treated as action methods in Asp .Net MVC. The methods which are
  • ย By using this we changing the default behavior.

Example

Both GetUserDetail are treated as action methods.

public ActionResult GetUserDetail(){
// some code inside here
return View();
}

public void GetUserDetail(){
// some code inside here
}

So overcome this we are using NonAction Attribute method

[NonAction]
public void GetUserDetail(){
// some code inside here
}

[NonAction]
public ActionResult GetUserDetail(){
// some code inside here
return View();
}

Now above two methods are treated as Non action method.ย