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.
Step 1
Step 2
Step 3
Step 4
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".
After you click on "Add a window", the wizard will open. Choose EF Designer from the database and click "Next".
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".
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".
After clicking on NEXT, another window will appear. Choose the database table name as shown in the below screenshot and click "Finish".
Entity Framework gets added and the respective class gets generated under the Models folder.
Step 5
A window will appear. Choose MVC5 Controller-Empty and click "Add".
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.

Controller without NonAction attribute
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MvcActionSelectors_Demo.Models;
- namespace MvcActionSelectors_Demo.Controllers
- {
- public class DepartmentController : Controller
- {
- private readonly EmployeeContext _dbContext=new EmployeeContext();
- public ActionResult Index()
- {
- var department = _dbContext.Departments.ToList();
- return View(department);
- }
- public string HelloWorld()
- {
- return "<h2>Hello World, Welcome to programming.</h2>";
- }
- }
- }
Controller with NonAction attribute
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MvcActionSelectors_Demo.Models;
- namespace MvcActionSelectors_Demo.Controllers
- {
- public class DepartmentController : Controller
- {
- private readonly EmployeeContext _dbContext=new EmployeeContext();
- public ActionResult Index()
- {
- var department = _dbContext.Departments.ToList();
- return View(department);
- }
- [NonAction]
- public string HelloWorld()
- {
- return "<h2>Hello World, Welcome to programming.</h2>";
- }
- }
- }
Step 6
Code for Index View
- @model IEnumerable<MvcActionSelectors_Demo.Models.Department>
- @{
- ViewBag.Title = "Index";
- }
- <h2>List of Department</h2>
- <table class="table table-bordered">
- <thead>
- <tr>
- <th>@Html.DisplayNameFor(m=>m.ID)</th>
- <th>@Html.DisplayNameFor(m=>m.DepartmentName)</th>
- </tr>
- </thead>
- <tbody>
- @foreach (var dep in Model)
- {
- <tr>
- <td>@dep.ID</td>
- <td>@dep.DepartmentName</td>
- </tr>
- }
- </tbody>
- </table>
Step 7
http://localhost:56100/Department/HelloWorld
Now, if you navigate to http://localhost:56100/Department/HelloWorld, you will get an error the resource cannot be found as shown below.


Join the conversation! Your thoughts help the community grow.