ASP .NET MVC NonAction meaning

白昼怎懂夜的黑 提交于 2019-12-18 18:37:13

问题


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
}

回答1:


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.




回答2:


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
}



回答3:


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




回答4:


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){
  }
}



回答5:


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!




回答6:


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.




回答7:


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.




回答8:


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.




回答9:


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/




回答10:


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



来源:https://stackoverflow.com/questions/6385599/asp-net-mvc-nonaction-meaning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!