AmbiguousMatchException

AmbiguousMatchException

本文介绍了如何避免两个控制器操作之间的 AmbiguousMatchException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

My unit tests have no issue with calling one or the other controller action, but my test html page throws a 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?

推荐答案

只有这样一种方法.

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

这篇关于如何避免两个控制器操作之间的 AmbiguousMatchException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 23:36