ASP.NET MVC: NonAction action filter
In ASP.NET MVC, every public method of controller is accessible via url regardless of return type, so if we have created any public method in controller which is not intended to serve as action method then also it is accessible via url. One solution is that keep this method as private or protected but some time we need to keep this method as public. This is where NonAction attribute comes in picture. First have a look at public method in following code.
Sample Code:
public string GetBlogName()
{
return "dotnetExpertGuide.com";
}
As we can see that we can access this method via url. Now mark this method with NonAction attribute as follow.
Attribute Usage: Only method
Sample Code:
[NonAction]
public string GetBlogName()
{
return "dotnetExpertGuide.com";
}
Now try to access this method via url. It will return HTTP 404 not found error. So whenever we want to create public method in controller which is not intended for action method or in other word which is not returning Action Result those method should be marked with NonAction action method selector attribute. NonAction is inherited from ActionMethodSelectorAttribute.
Check out ASP.NET MVC: Action filter series post to read about other available action filters.
You can follow me on twitter for latest link and update on ASP.NET & MVC.