ASP .NET MVC NonAction meaning

后端 未结 10 1713
没有蜡笔的小新
没有蜡笔的小新 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: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){
      }
    }
    

提交回复
热议问题