ASP .NET MVC NonAction meaning

后端 未结 10 1642
没有蜡笔的小新
没有蜡笔的小新 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:09

    I just used [NonAction] in our web api, to decorate a bunch of Controller Methods (endpoints) because we had a last minute decision that we will postpone the delivery of the specific endpoints.

    So it is useful, if you want to avoid exposing an API endpoint, but still want to keep the implementation for later.

    So I used this attribute and it saved me a lot of time.

    I will just remove it in the next release and this will simply be there!

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

    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.

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

    It is worth noting that the need to use [NonAction] applies only to public methods. Protected and private methods are not treated as actions. Since your Update/Delete/Insert methods are helpers for asd(), a private method would be more appropriate for your scenario:

    public ActionResult asd(string submitButton){
        switch(submitButton){
            case "Insert":
                return Insert();
            // bla bla bla
        }
    }
    
    ActionResult Insert(){
        // some code inside here
    }
    
    0 讨论(0)
  • 2021-01-03 18:14

    If you don't use the [NonAction] attribute then someone can call your action directly instead of having to go through the 'asd' function

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

    This is indicate that a controller method is not an action method.Example: [NonAction] public void IndexTest() { // Do some thing } this is very useful attribute when visibility of controller 's method cannot be changed to private.

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

    Firstly, think of an ActionResult simply as a particular type of construct that is returned by MVC, that happens to provide a particular convenience in terms of the way that ActionResult can be internally handled within the MVC framework. So the fact that something is an ActionResult doesn't necessarily mean "this should be publicly available". In fact, any public method in an MVC controller will be treated as an action method, whether or not it returns an ActionResult.

    So, simply having a return type that isn't an ActionResult will not necessarily prevent that method from being exposed as a publicly available action that can be called via a URL.

    There may be many reasons you don't want to expose a method as an action that can be invoked via a url, and in the case that you want to 'protect' against this, that's when you use the [NonAction'] attribute.

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