Non-Action Selector In ASP.NET MVC

Introduction

This article will explain the Non-Action Selectors in ASP.NET MVC. An action method is a public method in a controller that can be invoked using a URL. So, by default, if we have any public method in a controller then it can be invoked using a URL request. To restrict access to public methods in a Controller, a Non-Action attribute can be used. Non-Action is another built-in attribute which indicates that a public method of a Controller is not an action method. It is used when we  don't want that method to be treated as an action method. 

Please read my previous articles to learn more about Selectors in ASP.NET MVC. 
Let's see the Non-Action Selectors in action via this example.

Step 1

Open Visual Studio 2015 or an editor of your choice and create a new project.

Step 2

Choose "web application" project and give an appropriate name to your project.
 
Non Action Selector In ASP.NET MVC 

Step 3

Select "empty" template, check on the MVC box, and click OK.
 
Non Action Selector In ASP.NET MVC 

Step 4

Right-click on the Models folder and add a database model. Add Entity Framework now. For that, right-click on Models folder, select Add, then select New Item.
 
Non Action Selector In ASP.NET MVC 

You will get a window; from there, select Data from the left panel and choose ADO.NET Entity Data Model, give it the name EmployeeModel (this name is not mandatory, you can give any name) and click "Add".

Non Action Selector In ASP.NET MVC 

After you click on "Add a window", the wizard will open. Choose EF Designer from the database and click "Next".

Non Action Selector In ASP.NET MVC 

After clicking on "Next", a window will appear. Choose New Connection. Another window will appear. Add your server name - if it is local, then enter a dot (.). Choose your database and click "OK".

Non Action Selector In ASP.NET MVC 

The connection will be added. If you wish, save the connection name as you want. You can change the name of your connection below. It will save the connection in the web config. Now, click "Next".

Non Action Selector In ASP.NET MVC 

After clicking on NEXT, another window will appear. Choose the database table name as shown in the below screenshot and click "Finish".

Non Action Selector In ASP.NET MVC 

Entity Framework gets added and the respective class gets generated under the Models folder.

Non Action Selector In ASP.NET MVC 

Step 5

Right-click on Controllers folder add a controller.
 
Non Action Selector In ASP.NET MVC 

A window will appear. Choose MVC5 Controller-Empty and click "Add".

Non Action Selector In ASP.NET MVC 

After clicking on "Add", another window will appear with DefaultController. Change the name to HomeController and click "Add". The HomeController will be added under the Controllers folder. Don’t change the Controller suffix for all controllers, change only the highlight, and instead of Default, just change Home.

Non Action Selector In ASP.NET MVC

Controller without NonAction attribute

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MvcActionSelectors_Demo.Models;  
  7.    
  8. namespace MvcActionSelectors_Demo.Controllers  
  9. {  
  10.     public class DepartmentController : Controller  
  11.     {  
  12.         private readonly EmployeeContext _dbContext=new EmployeeContext();  
  13.    
  14.         public ActionResult Index()  
  15.         {  
  16.             var department = _dbContext.Departments.ToList();  
  17.             return View(department);  
  18.         }  
  19.    
  20.         public string HelloWorld()  
  21.         {  
  22.             return "<h2>Hello World, Welcome to programming.</h2>";  
  23.         }  
  24.     }  
  25. }  

Controller with NonAction attribute

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MvcActionSelectors_Demo.Models;  
  7.    
  8. namespace MvcActionSelectors_Demo.Controllers  
  9. {  
  10.     public class DepartmentController : Controller  
  11.     {  
  12.         private readonly EmployeeContext _dbContext=new EmployeeContext();  
  13.    
  14.         public ActionResult Index()  
  15.         {  
  16.             var department = _dbContext.Departments.ToList();  
  17.             return View(department);  
  18.         }  
  19.    
  20.         [NonAction]  
  21.         public string HelloWorld()  
  22.         {  
  23.             return "<h2>Hello World, Welcome to programming.</h2>";  
  24.         }  
  25.     }  
  26. }  

Step 6

Right-click on Index method in HomeController. The "Add View" window will appear with default index name checked (use a Layout page). Click on "Add".
 
Non Action Selector In ASP.NET MVC 

Code for Index View

  1. @model IEnumerable<MvcActionSelectors_Demo.Models.Department>  
  2. @{  
  3.     ViewBag.Title = "Index";  
  4. }  
  5.    
  6. <h2>List of Department</h2>  
  7. <table class="table table-bordered">  
  8.     <thead>  
  9.     <tr>  
  10.         <th>@Html.DisplayNameFor(m=>m.ID)</th>  
  11.         <th>@Html.DisplayNameFor(m=>m.DepartmentName)</th>  
  12.     </tr>  
  13.     </thead>  
  14.     <tbody>  
  15.     @foreach (var dep in Model)  
  16.     {  
  17.         <tr>  
  18.             <td>@dep.ID</td>  
  19.             <td>@dep.DepartmentName</td>  
  20.         </tr>  
  21.     }  
  22.     </tbody>  
  23. </table>  

Step 7

Build and run the project using Ctrl+F5.

http://localhost:56100/Department/HelloWorld

Non Action Selector In ASP.NET MVC 

Now, if you navigate to http://localhost:56100/Department/HelloWorld, you will get an error the resource cannot be found as shown below.

Non Action Selector In ASP.NET MVC
In general, it’s a bad practice 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, we want to have public methods in a controller and we don’t want to treat them as actions, then use Non-Action attribute. 


Similar Articles