我怎样才能避免出现两个控制器动作之间AmbiguousMatchException? [英] How can I avoid AmbiguousMatchException between two controller actions?

查看:201
本文介绍了我怎样才能避免出现两个控制器动作之间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?

推荐答案

只要有这样的一种方法。

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

这篇关于我怎样才能避免出现两个控制器动作之间AmbiguousMatchException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆