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.