In Asp.Net applications with no MVC framework, user interacts with pages along with events of the page and the controls. In contrast with ASP.NET MVC applications, user interacts with controllers and action methods.
In Asp.Net MVC framework, we have Action Methods which can be called by the user while visiting the site. By default, Asp.Net MVC framework treats all public methods as action methods. These methods can directly be called via URL. If we specify Action Method with Non-action attribute then these methods would not be allowed to be called while visiting the site means the user would not be able to access these methods.
We can see it via sample:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public string HelloWorld()
{
return "Hello World!";
}
[NonAction]
public string Hello()
{
return "Hello!";
}
}
Note: We can call home/HelloWorld in the browser, but if we attempt to call home/Hello in the browser, it throws error of 404 means resource not found.
Main Purpose of Public Non-Action Methods is
To restrict the access to non-action method while visiting the site.
Private vs Non-Action Method
We can use Private method instead of Non-Action method but if we use Private method then the other controllers would not be able to access these methods but if we provide Non-Action attribute to the Public method then the other controllers would be able to access the same.
0 Comment(s)