Non-Action Selector in ASP.NET MVC

Non-Action Selector in ASP.NET MVC Application

In this article, I am going to discuss the Non-Action Selector in ASP.NET MVC Application with Examples. Please read our previous article before proceeding to this article as we are going to work on the same example that we worked on in our previous two articles. In our previous article, we discussed the Action Verb Action Selector in ASP.NET MVC Application with examples.

Why do we need a Non-Action Selector in the ASP.NET MVC Application?

By default, in ASP.NET MVC, an action method is a public method in a controller that can be invoked using a URL request. But in some scenarios, we need to restrict the public methods of a controller to be invoked by a URL i.e. we don’t want the action method to be invoked using URL request.

To restrict access to the public methods in a controller, we need to decorate the action method with the Non-Action attribute. The Non-Action is another built-in Action Selector Attribute, which indicates that a public method of a Controller is not an action method. It is used when we want that method shouldn’t be treated as an action method.

Understanding Non-Action Attribute in the ASP.NET MVC Application with an Example.
public class HomeController : Controller
{
    [HttpGet]
    public string Method1()
    {
        return "<h1>Method 1 Invoked</h1>";
    }

    [HttpGet]
    public string Method2()
    {
        return "<h1>Method 2 Invoked</h1>";
    }
}

As you can see in the above code, we have 2 public methods i.e. Method1() and Method2() in HomeController. As the above two methods are public methods, so, we can access these two methods using the below URL.

Method1 can be invoked using URL /Home/Method1
Method2 can be invoked using URL /Home/Method2

Let’s say Method2() is doing some internal work, and we don’t want it to be invoked using a URL request. To achieve this, we need to decorate Method2() with the NonAction attribute as shown in the below code.

[NonAction]
public string Method2()
{
    return "<h1>Method 2 Invoked</h1>";
}

Now, if we navigate to URL /Home/Method2, we will get an error saying – The resource cannot be found as shown in the image below.

Non-Action Selector in ASP.NET MVC Application

Another way to restrict access to methods in a controller is by making them private. 
private string Method2()
{
    return "<h1>Method 2 Invoked</h1>";
}

In general, it’s a bad design to have a public method in a controller that is not an action method. If we have any such method for performing business calculations, it should be somewhere in the model and not in the controller. However, if for some reason, if you want to have public methods in a controller and you don’t want to treat them as actions, then use the Non-Action Selector Attribute in ASP.NET MVC Application. 

In the next article, I am going to discuss Data Annotation in the ASP.NET MVC Application with Examples. In this article, I try to explain the Non-Action Selector in ASP.NET MVC Application with Example. I hope this Non-Action Selector in the ASP.NET MVC article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.