你可以重载的ASP.NET MVC控制器的方法呢? [英] Can you overload controller methods in ASP.NET MVC?

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

问题描述

我很好奇,看看你是否能在超载ASP.NET MVC控制器方法。每当我尝试,我得到下面的错误。这两种方法接受不同的参数。这是不是说不能做?


  

采取行动的当前请求的控制器类型的MyMethod'myController的是下面的操作方法之间模棱两可的:



解决方案

可以,如果你希望你的code做超载使用属性。

  [ActionName(MyOverloadedName)]

但是,你将不得不使用不同的动作名称相同的HTTP方法(如其他人所说的)。所以这是在这一点上只是语义。你愿意在code或你的属性有名字吗?

菲尔具有与此相关的一篇文章:<一href=\"http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx\">http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx

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?

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")]

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 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天全站免登陆