如何在控制器中覆盖动作方法?任何人都可以用一个小例子来解释。还有一件事要问,如果没有虚拟关键字,我们可以做到这一点吗?
据我了解您的问题,这些是答案:
第一个答案:
不可能有两个具有相同名称但结果也不同的控制器动作:
例如:
ActionResult YourAction() { ... }
FileContentResult YourAction() { ... }
在MVC中,您还可以执行以下操作:
[HttpGet]
[ActionName("AnyAction")]
ActionResult YourAction(firstModel model1) { ... }
[HttpPost]
[ActionName("AnyAction")]
FileContentResult YourAction(secondModel model1) { ... }
这里的主要思想是,您可以使用ActionNameAttribute来使用相同的名称来命名多个操作方法。
-------------------------------------------------- - - - - - - - 或者 - - - - - - - - - - - - - - - - - - ---------------------------
第二个答案:
[NonAction]
public override ActionResult YourAction(FormCollection form)
{
// do nothing or throw exception
}
[HttpPost]
public ActionResult YourAction(FormCollection form)
{
// your implementation
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句