ASP.NET MVC NonAction 含义

发布于 2024-11-15 22:37:14 字数 525 浏览 5 评论 0原文

谁能告诉我为什么应该使用 NonAction 属性?我的意思是说我有一个包含多个提交值的表单:更新、删除或插入。由于所有提交按钮都具有相同的共同形式,因此我在控制器内切换提交值并采取相应的操作。

像这样:

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

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

再一次,为什么我应该使用 NonAction 而不是这样的东西:

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

Can anybody please tell me why should I use the NonAction attribute? 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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

星星的軌跡 2024-11-22 22:37:14

您可以省略 NonAction 属性,但该方法仍然可以作为操作方法调用。

从 MSDN 站点 (ref):

默认情况下,MVC 框架将控制器类的所有公共方法视为操作方法。如果您的控制器类包含公共方法并且您不希望它成为操作方法,则必须使用 NonActionAttribute 属性标记该方法。

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.

浅笑轻吟梦一曲 2024-11-22 22:37:14

值得注意的是,需要使用[NonAction]仅适用于公共方法。受保护的方法和私有方法不被视为操作。由于您的 Update/Delete/Insert 方法是 asd() 的帮助程序,因此私有方法会更合适对于您的场景:

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

ActionResult Insert(){
    // some code inside here
}

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
}
不忘初心 2024-11-22 22:37:14

我只是在我们的 Web api 中使用了 [NonAction] 来装饰一堆控制器方法(端点),因为我们在最后一刻决定推迟特定端点的交付。

因此,如果您想避免暴露 API 端点,但仍想保留实现以供以后使用,那么它很有用。

所以我使用了这个属性,它节省了我很多时间。

我将在下一个版本中将其删除,并且它就在那里!

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!

舂唻埖巳落 2024-11-22 22:37:14

阅读 Haack 的文章< /a>

控制器类中的任何公共方法都可以通过 URL 调用。

有时您可能需要避免这种情况。例如,如果您实现了某个接口,并且您可能不想调用该公共方法,则可以将其标记为 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){
  }
}

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){
  }
}
旧瑾黎汐 2024-11-22 22:37:14

如果您不使用 [NonAction] 属性,那么有人可以直接调用您的操作,而不必通过“asd”函数

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

后来的我们 2024-11-22 22:37:14

NonAction 属性使操作无法从导航栏访问。例如,如果您有一个删除数据库中项目的操作,则必须添加 NonAction 属性以使其无法被用户访问。

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.

雨轻弹 2024-11-22 22:37:14

首先,将 ActionResult 简单地视为 MVC 返回的特定类型的构造,它恰好在 MVC 框架内内部处理 ActionResult 的方式方面提供了特殊的便利。因此,某物是 ActionResult 的事实并不一定意味着“这应该是公开可用的”。事实上,MVC 控制器中的任何公共方法都将被视为操作方法,无论它是否返回 ActionResult

因此,如果返回类型不是 ActionResult,则不一定会阻止该方法公开为可通过 URL 调用的公开可用操作。

您不想将方法公开为可通过 url 调用的操作的原因可能有很多,如果您想“防止”这种情况,则需要使用 [NonAction'] 属性。

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.

神爱温柔 2024-11-22 22:37:14

这表明控制器方法不是操作方法。示例: [NonAction] public void IndexTest() { // Do some thing } 当控制器方法的可见性无法更改为私有时,这是非常有用的属性。

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.

零度℉ 2024-11-22 22:37:14

如果您不想调用某些操作方法,那么您必须使用属性 [NonAction] 标记它,或者通过将其设为私有,

public ActionResult Index(){
    return View();
}

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

您可以复制代码并粘贴它并查看结果。谢谢

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文