[英]ASP.NET MVC - Overriding an action with differing parameters
I have a controller that inherits from a base controller.我有一个继承自基本控制器的控制器。 Both have an edit (post) action which take two arguments:
两者都有一个带有两个参数的编辑(发布)操作:
On Base controller:在基本控制器上:
[HttpPost]
public virtual ActionResult Edit(IdType id, FormCollection form)
And in the derived controller:在派生控制器中:
[HttpPost]
public ActionResult Edit(int id, SomeViewModel viewModel)
If I leave it like this I get an exception because there is an ambiguous call.如果我这样离开它,我会得到一个例外,因为有一个模棱两可的调用。 However, I can't use
override
on the derived action, because the method signatures don't exactly match.但是,我不能在派生操作上使用
override
,因为方法签名不完全匹配。 Is there anything I can do here?有什么我可以在这里做的吗?
As addition to Developer Art's answer a workaround would be:除了开发人员艺术的答案,解决方法是:
leave the base method as it is and in your derived class implement the base method and annotate it with [NonAction]
保持基本方法
[NonAction]
并在您的派生类中实现基本方法并使用[NonAction]
对其进行注释
[NonAction]
public override ActionResult Edit(IdType id, FormCollection form)
{
// do nothing or throw exception
}
[HttpPost]
public ActionResult Edit(int id, SomeViewModel viewModel)
{
// your implementation
}
I'd chain it:我会把它链接起来:
On Base controller:在基本控制器上:
[HttpPost]
public virtual ActionResult Edit(IdType id, FormCollection form)
And in the derived controller:在派生控制器中:
[HttpPost]
public virtual ActionResult Edit(IdType id, FormCollection form)
{
var newId = //some enum? transform
var boundModel = UpdateModel(new SomeViewModel(), form);
return Edit( newId, boundModel );
}
[HttpPost]
public ActionResult Edit(int id, SomeViewModel viewModel)
I haven't tested this, passing a Post method to another Post should work.我还没有测试过,将 Post 方法传递给另一个 Post 应该可以工作。 There could be security implications this way.
这种方式可能存在安全隐患。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.