@Blog.Author(Nandip Makwana) .LearningExperience(ASP.NET, ASP.NET MVC, IIS, jQuery & Technology Surrounding it...)

October 23, 2012 comment

ASP.NET MVC: NonAction action filter

ASP.NET MVC: NonAction

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.

comments powered by Disqus

Featured Content

Resources & Tools

About Nandip Makwana

Nandip Makwana is passionate about digital world and web. He completed his Masters in Computer Application in June 2011. Currently he is working as a Software Engineer. He has shown great promise and command over ASP.NET and technologies surrounding it during his academic years and professorial life...continue reading