ASP.NET MVC NonAction 含义
谁能告诉我为什么应该使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以省略
NonAction
属性,但该方法仍然可以作为操作方法调用。从 MSDN 站点 (ref):
You can omit the
NonAction
attribute but then the method is still invokable as action method.From the MSDN site (ref):
值得注意的是,需要使用
[NonAction]
仅适用于公共方法。受保护的方法和私有方法不被视为操作。由于您的Update
/Delete
/Insert
方法是asd()
的帮助程序,因此私有方法会更合适对于您的场景: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 yourUpdate
/Delete
/Insert
methods are helpers forasd()
, a private method would be more appropriate for your scenario:我只是在我们的 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!
阅读 Haack 的文章< /a>
有时您可能需要避免这种情况。例如,如果您实现了某个接口,并且您可能不想调用该公共方法,则可以将其标记为
NonAction
Reading Haack's Article
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
如果您不使用 [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
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.首先,将 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.
这表明控制器方法不是操作方法。示例: [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.
如果您不想调用某些操作方法,那么您必须使用属性
[NonAction]
标记它,或者通过将其设为私有,您可以复制代码并粘贴它并查看结果。谢谢
If you did not want to invoke some action methods then you have to mark it with a attribute
[NonAction]
or by making it privateyou can copy the code and paste it and see the result.thanks