Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I avoid AmbiguousMatchException between two controller actions?

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.

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

like image 721
Sailing Judo Avatar asked Apr 08 '09 22:04

Sailing Judo


2 Answers

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);
}
like image 63
Schotime Avatar answered Oct 14 '22 03:10

Schotime


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

like image 45
Brandon Avatar answered Oct 14 '22 04:10

Brandon