C# 如何避免两个控制器操作之间的 AmbiguousMatchException?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/732205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:01:12  来源:igfitidea点击:

How can I avoid AmbiguousMatchException between two controller actions?

c#.netasp.net-mvccontroller

提问by Sailing Judo

I have two controller actions with the same name but with different method signatures. They look like this:

我有两个具有相同名称但具有不同方法签名的控制器操作。它们看起来像这样:

    //
    // GET: /Stationery/5?asHtml=true
    [AcceptVerbs(HttpVerbs.Get)]
    public ContentResult Show(int id, bool asHtml)
    {
        if (!asHtml)
            RedirectToAction("Show", id);

        var result = Stationery.Load(id);
        return Content(result.GetHtml());
    }

    //
    // GET: /Stationery/5
    [AcceptVerbs(HttpVerbs.Get)]
    public XmlResult Show(int id)
    {
        var result = Stationery.Load(id);
        return new XmlResult(result);
    }

My unit tests have no issue with calling one or the other controller action, but my test html page throws a System.Reflection.AmbiguousMatchException.

我的单元测试在调用一个或另一个控制器操作时没有问题,但我的测试 html 页面抛出 System.Reflection.AmbiguousMatchException。

<a href="/Stationery/1?asHtml=true">Show the stationery Html</a>
<a href="/Stationery/1">Show the stationery</a>

What needs to change to make this work?

需要改变什么才能使这项工作?

采纳答案by Schotime

Just have one method like this.

只要有这样一种方法。

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Show(int id, bool? asHtml)
{
    var result = Stationery.Load(id);

    if (asHtml.HasValue && asHtml.Value)
        return Content(result.GetHtml());
    else
        return new XmlResult(result);
}

回答by Brandon

Heres a link you may find userful.It talks about overloading the MVC Controllers.

这是一个您可能会觉得有用的链接。它讨论了重载 MVC 控制器。

回答by J.W.

There are two ways to address this:

有两种方法可以解决这个问题:

1> Change the method name. 2> Provide different ActionName attributes to the two methods. You can define your own attribute.

1> 更改方法名称。2> 为两个方法提供不同的ActionName 属性。您可以定义自己的属性。

回答by Daniel A. White

There is the ActionNameattribute. Take a look.

ActionName属性。看一看。

回答by Ian Mercer

To overcome this problem you can write an ActionMethodSelectorAttributethat examines the MethodInfofor each action and compares it to the posted Form values and then rejects any method for which the form values don't match (excluding the button name, of course).

为了克服这个问题,您可以编写一个ActionMethodSelectorAttribute检查MethodInfo每个操作的 ,并将其与发布的表单值进行比较,然后拒绝表单值不匹配的任何方法(当然不包括按钮名称)。

Here's an example:- http://blog.abodit.com/2010/02/asp-net-mvc-ambiguous-match/

这是一个例子:- http://blog.abodit.com/2010/02/asp-net-mvc-ambiguous-match/

You could also make a simpler ActionMethodSelectorAttributewhich looks only at the submit button name but that would tie your controller and view more closely.

您还可以制作一个更简单的ActionMethodSelectorAttribute,只查看提交按钮名称,但这会绑定您的控制器并更紧密地查看。