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