Web
Analytics
non action attribute in mvc | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

It’s a attribute that which is used on the methods who are defined by  public access modifier.Actually MVC Framework treats all public methods as URL but in case if you don’t want this then you have to make a method buy decorated it by non action attribute.Same may be achieved by making method as private access modifier.

Example of NonAction Attribute is given below.





 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NonActionMethod.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        //Following method is declared by public access modifier but it will be access 
        //because it is decorated by nonaction attribute
        //When we tried to access it then browser will show http 404 error
        
        [NonAction]
        public ActionResult nonurlfunction1()
        {
            return Content("It should not be called by URL");
        }
        //Following Method is also non accessible due to private non access modifier
        private ActionResult nonurlfunction2()
        {
            return Content("It should not be called by URL");
        }
    }
}