Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC ambiguous action methods
    primarykey
    data
    text
    <p>I have two action methods that are conflicting. Basically, I want to be able to get to the same view using two different routes, either by an item's ID or by the item's name and its parent's (items can have the same name across different parents). A search term can be used to filter the list.</p> <p>For example...</p> <pre><code>Items/{action}/ParentName/ItemName Items/{action}/1234-4321-1234-4321 </code></pre> <p>Here are my action methods (there are also <code>Remove</code> action methods)...</p> <pre><code>// Method #1 public ActionResult Assign(string parentName, string itemName) { // Logic to retrieve item's ID here... string itemId = ...; return RedirectToAction("Assign", "Items", new { itemId }); } // Method #2 public ActionResult Assign(string itemId, string searchTerm, int? page) { ... } </code></pre> <p>And here are the routes...</p> <pre><code>routes.MapRoute("AssignRemove", "Items/{action}/{itemId}", new { controller = "Items" } ); routes.MapRoute("AssignRemovePretty", "Items/{action}/{parentName}/{itemName}", new { controller = "Items" } ); </code></pre> <p>I understand why the error is occurring, since the <code>page</code> parameter can be null, but I can't figure out the best way to resolve it. Is my design poor to begin with? I've thought about extending <code>Method #1</code>'s signature to include the search parameters and moving the logic in <code>Method #2</code> out to a private method they would both call, but I don't believe that will actually resolve the ambiguity.</p> <p>Any help would be greatly appreciated.</p> <hr> <p><strong>Actual Solution</strong> (based on Levi's answer)</p> <p>I added the following class...</p> <pre><code>public class RequireRouteValuesAttribute : ActionMethodSelectorAttribute { public RequireRouteValuesAttribute(string[] valueNames) { ValueNames = valueNames; } public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) { bool contains = false; foreach (var value in ValueNames) { contains = controllerContext.RequestContext.RouteData.Values.ContainsKey(value); if (!contains) break; } return contains; } public string[] ValueNames { get; private set; } } </code></pre> <p>And then decorated the action methods...</p> <pre><code>[RequireRouteValues(new[] { "parentName", "itemName" })] public ActionResult Assign(string parentName, string itemName) { ... } [RequireRouteValues(new[] { "itemId" })] public ActionResult Assign(string itemId) { ... } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload