如何避免两个控制器操作之间存在明显的匹配异常?

时间:2022-04-29 21:13:28

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. definousmatchexception。

<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?

需要改变什么来做这项工作?

5 个解决方案

#1


11  

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);
}

#2


6  

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

这是一个你可以找到userful的链接。它讨论了重载MVC控制器。

#3


1  

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.

改变方法名。>为这两个方法提供了不同的ActionName属性。您可以定义自己的属性。

#4


0  

There is the ActionName attribute. Take a look.

这里是ActionName属性。看一看。

#5


0  

To overcome this problem you can write an ActionMethodSelectorAttribute that examines the MethodInfo for 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来检查每个动作的方法,并将它与已发布的表单值进行比较,然后拒绝任何表单值不匹配的方法(当然不包括按钮名)。

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 ActionMethodSelectorAttribute which looks only at the submit button name but that would tie your controller and view more closely.

您还可以创建一个更简单的ActionMethodSelectorAttribute,它只查看提交按钮名称,但这会使您的控制器和视图更加紧密。

#1


11  

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);
}

#2


6  

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

这是一个你可以找到userful的链接。它讨论了重载MVC控制器。

#3


1  

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.

改变方法名。>为这两个方法提供了不同的ActionName属性。您可以定义自己的属性。

#4


0  

There is the ActionName attribute. Take a look.

这里是ActionName属性。看一看。

#5


0  

To overcome this problem you can write an ActionMethodSelectorAttribute that examines the MethodInfo for 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来检查每个动作的方法,并将它与已发布的表单值进行比较,然后拒绝任何表单值不匹配的方法(当然不包括按钮名)。

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 ActionMethodSelectorAttribute which looks only at the submit button name but that would tie your controller and view more closely.

您还可以创建一个更简单的ActionMethodSelectorAttribute,它只查看提交按钮名称,但这会使您的控制器和视图更加紧密。