你能在 ASP.NET MVC 中重载控制器方法吗? [英] Can you overload controller methods in ASP.NET MVC?

查看:38
本文介绍了你能在 ASP.NET MVC 中重载控制器方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇您是否可以在 ASP.NET MVC 中重载控制器方法.每当我尝试时,都会收到以下错误.这两种方法接受不同的参数.这是做不到的吗?

I'm curious to see if you can overload controller methods in ASP.NET MVC. Whenever I try, I get the error below. The two methods accept different arguments. Is this something that cannot be done?

当前对控制器类型MyController"的操作MyMethod"的请求在以下操作方法之间不明确:

The current request for action 'MyMethod' on controller type 'MyController' is ambiguous between the following action methods:

推荐答案

如果您希望代码进行重载,可以使用该属性.

You can use the attribute if you want your code to do overloading.

[ActionName("MyOverloadedName")]

但是,您必须为相同的 http 方法使用不同的操作名称(正如其他人所说).所以在这一点上它只是语义.您更愿意在代码或属性中使用名称吗?

But, you'll have to use a different action name for the same http method (as others have said). So it's just semantics at that point. Would you rather have the name in your code or your attribute?

Phil 有一篇与此相关的文章:http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx

Phil has an article related to this: http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx

这篇关于你能在 ASP.NET MVC 中重载控制器方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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