ActionName and NonAction Attribute in ASP.Net MVC

ActionName and NonAction attribute in MVC is the attribute applied to an action method. These attribute helps route engine to execute correct Action method and the respective View.

ActionName Attribute

ActionName attribute actually provide a alias to the action method, i.e. You can give a different name to your Action method than what is given exactly.

Let's demonstrate this by sample code -


        public class HomeController : Controller

    {

        [ActionName("Test1")]

        public ActionResult Test()

        {

            return View();

        }

     }
 
 In above code you may notice an attribute ActionName added to the Action method 'Test'

So, if your URL pattern in route is  /{controller}/{action}, then URL for above Action method is -

/Home/Test1 and not /Home/Test

NonAction Attribute

If an Action method is decorated with NonAction attribute, it means this method will not be treated as action method and URL with that method will throw 404.


         public class HomeController : Controller

    {

        [NonAction]

        public ActionResult Test()

        {

            return View();

        }

    }


As you can see, in above sample code Action method Test is decorated with attribute NonAction.

It means you can not access this Action Method from URL like /Home/Test. It will throw 404 error.

You may also like these blogs-

Comments

Popular

What's new in C# 6.0 language - All new features of C# 6.0 for developers

SharePoint Interview Questions and Answers