Site icon Dotnet Helpers

NonActionAttribute Class in MVC

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 :

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. 

Exit mobile version