如何在MVC控制器中覆盖动作? [英] How to override an action in mvc controller?

查看:57
本文介绍了如何在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控制器中覆盖动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆