简体   繁体   English

具有不同返回类型的操作方法的相同名称

[英]Same Name For Action Methods With Different Return Types

I've two action methods in my controller, both are called on POST request but have different return type: 我的控制器中有两个动作方法,都是在POST请求上调用但具有不同的返回类型:

public JsonResult AJAXCreate()

public string AJAXCreateNSave([Bind(Exclude = "Id, OrderItems")]Order order)

When I rename the second one to AJAXCreate, it is not called at all. 当我将第二个重命名为AJAXCreate时,根本不会调用它。 I want to use same name for both action methods. 我想为两种动作方法使用相同的名称。

Much like operations in WCF services, no two actions can have the same name, unless they target different HTTP verbs, eg: 与WCF服务中的操作非常相似,没有两个操作可以具有相同的名称,除非它们针对不同的HTTP谓词,例如:

public ActionResult MyAction() { }

[HttpPost]
public ActionResult MyAction(MyModel model) { }

If you try and use two actions with the same name, MVC doesn't know which action to select. 如果您尝试使用具有相同名称的两个操作,则MVC不知道要选择哪个操作。

Overloaded methods are not allowed in ASP.NET MVC without an attribute to specify a different action name. 如果没有用于指定其他操作名称的属性,ASP.NET MVC中不允许重载的方法。 Check out this similar question and answer: Can you overload controller methods in ASP.NET MVC? 看看这个类似的问题和答案: 你能在ASP.NET MVC中重载控制器方法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM