如何在MVC控制器中覆盖动作?

编程入门 行业动态 更新时间:2024-10-19 18:15:05
本文介绍了如何在MVC控制器中覆盖动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在控制器中覆盖动作方法?任何人都可以用一个小例子来解释.还有一件事要问,如果没有虚拟关键字,我们可以做到这一点吗?

How to override an action method in a controller? Can anyone explain with a small example. And one more thing to ask , can we do this without virtual keyword?

推荐答案

据我所知,这些是答案:

As far as i m understanding your question these are the answers :

第一个答案:

不可能有两个具有相同名称但结果也不同的控制器动作:

it's not possible to have two controller actions with the same name but with a different result also:

例如:

ActionResult YourAction() { ... } FileContentResult YourAction() { ... }

在MVC中,您也可以执行以下操作:

In MVC you can also do this :

[HttpGet] [ActionName("AnyAction")] ActionResult YourAction(firstModel model1) { ... } [HttpPost] [ActionName("AnyAction")] FileContentResult YourAction(secondModel model1) { ... }

这里的主要思想是,您可以使用ActionNameAttribute来命名具有相同名称的多个操作方法.

The main idea here is that you can use the ActionNameAttribute to name several action methods with the same name.

---------------------------------------------------- - - - - - - - -或者 - - - - - - - - - - - - - - - -------------------------------

----------------------------------------------------------------OR--------------------------------------------------------------

第二个答案:

[NonAction] public override ActionResult YourAction(FormCollection form) { // do nothing or throw exception } [HttpPost] public ActionResult YourAction(FormCollection form) { // your implementation }

更多推荐

如何在MVC控制器中覆盖动作?

本文发布于:2023-11-24 21:02:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626810.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:器中   动作   如何在   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!