ASP .NET MVC NonAction meaning

后端 未结 10 1690
没有蜡笔的小新
没有蜡笔的小新 2021-01-03 17:39

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 s

相关标签:
10条回答
  • 2021-01-03 18:23

    If you did not want to invoke some action methods then you have to mark it with a attribute [NonAction] or by making it private

    public ActionResult Index(){
        return View();
    }
    
    [NonAction]
    public ActionResult Countries(List<string>countries){
        return View(countries);
    }
    

    you can copy the code and paste it and see the result.thanks

    0 讨论(0)
  • 2021-01-03 18:25

    Reading Haack's Article

    Any public method in a controller class is callable via URL.

    Sometimes you may need to avoid this. For example, if you implement some interface and you may not want to call that public method you can mark as NonAction

    public interface IEmployee
    {
     void Save(Employee e);
     bool Validate(Employee e);
    }
    
    public class EmployeeController:Controller, IEmployee
    {
      public void Save(Employee e){
      }
    
      [NonAction]
      public void Validate(Employee e){
      }
    }
    
    0 讨论(0)
  • 2021-01-03 18:27

    It's an attribute which is used on the methods which are defined by public access modifiers. Actually MVC Framework treats all public methods as URLs but in case you don't want this then you have to decorate a method with the non action attribute. The same thing may be achieved by making the method private.

    An example of NonAction Attribute is given below. http://yogeshdotnet.com/non-action-attribute-in-mvc/

    0 讨论(0)
  • 2021-01-03 18:28

    NonAction attribute makes an action non accessible from the navigation bar. For example if you have an action which deletes items in database, you have to add the NonAction attribute to make it not accessible by users.

    0 讨论(0)
提交回复
热议问题