简体   繁体   English

模糊调用asp.net mvc动作方法

[英]ambiguous call asp.net mvc action methods

Can someone explain why POST call to the following actions is ambiguous? 有人可以解释为什么POST调用以下操作是不明确的? They have different set of parameters? 他们有不同的参数集?

[RequireRequestValueAttribute("setID")]
public ActionResult Add(int setID){}

[HttpPost]
public ActionResult Add(TypeModel model, int? queueID) {}

The issue only occurs when using the RequireRequestValueAttribute attribute , which I am using because I wanted to add another method for a Get call with different set of parameters. 只有在使用我正在使用的RequireRequestValueAttribute属性时才会出现此问题 ,因为我想为具有不同参数集的Get调用添加另一个方法。

Following is the implementation of that that I am using, found on another stackoverflow question: 以下是我正在使用的实现,在另一个stackoverflow问题上找到:

public class RequireRequestValueAttribute : ActionMethodSelectorAttribute
{
    public RequireRequestValueAttribute(string valueName)
    {
        ValueName = valueName;
    }
    public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
    {
        return (controllerContext.HttpContext.Request[ValueName] != null);
    }
    public string ValueName { get; private set; }
} 

It's because in C# it is forbidden to have two methods with the same name and parameter types. 这是因为在C#中禁止有两个具有相同名称和参数类型的方法。 This has nothing to do with ASP.NET MVC. 这与ASP.NET MVC无关。 You should rename one of the two Add actions that could be invoked by GET. 您应该重命名可以由GET调用的两个 Add操作之一。

You cannot have two action names with the same name which can be invoked with the same verb (in your case GET). 您不能拥有两个具有相同名称的动作名称,可以使用相同的动词调用(在您的情况下为GET)。 You need to either rename one of them or use another HTTP verb as you did with your POST action. 您需要重命名其中一个或使用另一个HTTP动词,就像您对POST操作所做的那样。


UPDATE: 更新:

You could try introducing the HTTP verb in the custom action selector attribute: 您可以尝试在自定义操作选择器属性中引入HTTP谓词:

public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
    return controllerContext.HttpContext.Request[ValueName] != null &&
           controllerContext.HttpContext.Request.HttpMethod == "GET";
}

but quite frankly I wouldn't use a custom action selector to validate whether are request parameter is present or not. 但坦率地说,我不会使用自定义操作选择器来验证请求参数是否存在。 Route constraints or data annotations seem far more appropriate for this task. 路径约束或数据注释似乎更适合此任务。

Do yourself a huge favor. 做一个巨大的帮助。 Download the source for ASP.NET MVC. 下载ASP.NET MVC的源代码。 (Actually, you should do this anytime you have the luxury accessing the source code.) Set it up to debug and step through the part you are having trouble with. (实际上,您应该在访问源代码时随时执行此操作。)将其设置为调试并逐步完成您遇到问题的部分。 I cannot tell you how many times this has cleared up a problem like this for me. 我无法告诉你这次为我解决了这样的问题多少次。 You will get a much better understanding of what is actually going on that you would otherwise have, and it can be really surprising what you find in some cases. 您将更好地了解实际上会发生什么,并且在某些情况下您会发现它真的很令人惊讶。 I have in the past asked questions here, gotten 'workable' solutions, only to discover that there was a much easier, more elegant way of resolving the problem. 我过去曾在这里提出过问题,得到了“可行”的解决方案,但却发现有更简单,更优雅的方法来解决问题。

OK to answer my own question, it was my stupidity! 好的回答我自己的问题,这是我的愚蠢!

My get method has the setID parameter and because it is in the URL, of course this will also be in the post, and therefore RequireRequestValueAttribute was returning TRUE for IsValidForRequest for both methods . 我的get方法有setID参数,因为它在URL中, 当然这也将在post中,因此对于两种方法RequireRequestValueAttribute都为IsValidForRequest返回TRUE I got around it by adding a [HttpGet] attribute to the Get method so things will never get posted to it. 我通过在Get方法中添加[HttpGet]属性来解决它,所以事情永远不会发布到它。

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

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