Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Default base class for mvc controllers, <code>Controller</code> uses <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.actioninvoker.aspx" rel="noreferrer">ActionInvoker</a> to select which action to invoke. First the action is <strong>selected</strong>, by default from the RouteData["action"] value, and then all the model binding and validation for the selected action's parameters occur. That's why when the invoker sees two actions with the same name and same attributes for selecting, it fires error, as it cannot distinguish between two.</p> <p>But there's builtin way to manage action selecting logic, that is by using attributes derived from <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.actionmethodselectorattribute.aspx" rel="noreferrer">ActionMethodSelector class</a>. First you create class derived from it, which contains logic for invoking action. In your case</p> <blockquote> <p>it would use the parameterless Register action if there was no registrationToken GET parameter passed in, otherwise it would use the 2nd.</p> </blockquote> <pre><code> public class RegistrationActionTokenAttribute : ActionMethodSelectorAttribute { public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo) { if (controllerContext.HttpContext.Request.QueryString.AllKeys.Contains("registrationToken")) { return true; } return false; } } </code></pre> <p>I implemented demonstrational logic that second action should be marked valid for selection, if querystring contains parameter "registrationToken". The ony thing left is to decorate your second method with this attribute</p> <pre><code>[RegistrationActionToken] public virtual ActionResult Register (Guid registrationToken) {...} </code></pre> <p>And the error is gone. Moreover, controller now selects correct action, depending on query string parameter</p>
 

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