加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何避免两个控制器动作之间的AmbiguousMatchException?

发布时间:2020-12-15 07:39:03 所属栏目:百科 来源:网络整理
导读:我有两个具有相同名称但具有不同方法签名的控制器操作.他们看起来像这样: // // GET: /Stationery/5?asHtml=true [AcceptVerbs(HttpVerbs.Get)] public ContentResult Show(int id,bool asHtml) { if (!asHtml) RedirectToAction("Show",id); var result =
我有两个具有相同名称但具有不同方法签名的控制器操作.他们看起来像这样:
//
    // 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.

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

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

解决方法

只需一个这样的方法.
[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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读