ASP .NET MVC NonAction meaning

Shaokan picture Shaokan · Jun 17, 2011 · Viewed 55.8k times · Source

Can anybody please tell me why should I use NonAction? I mean say I have a form with several submit values: Update, Delete or Insert. Since all the submit buttons have the same form in common I'm switching the submit value inside the controller and act accordingly.

Like this:

public ActionResult asd(string submitButton){
     switch(submitButton){
         case "Insert":
             return Insert();
         // bla bla bla
     }
}

[NonAction]
public ActionResult Insert(){
    // some code inside here
    return View();
}

Once again, why should I use NonAction instead of something like this:

public void Insert(){
    // some code inside here
}

Answer

ReFocus picture ReFocus · Jun 17, 2011

You can omit the NonAction attribute but then the method is still invokable as action method.

From the MSDN site (ref):

By default, the MVC framework treats all public methods of a controller class as action methods. If your controller class contains a public method and you do not want it to be an action method, you must mark that method with the NonActionAttribute attribute.