Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Others have suggested many things already, but let me bring something else to the table here.</p> <h2>Action Method Selector</h2> <p>In order to keep your controller actions clean, you can write an action method selector attribute to create two simple actions:</p> <pre><code>[ActionName("Index")] public ActionResult IndexNonActivated(int id) { ... } [ActionName("Index")] [UserAccountActivated] public ActionResult IndexActivated(int id) { ... } </code></pre> <p>This way you don't deal with checking code in your actions keeping them really thin. Selector filter will make sure that correct action will get executed related to user account activation state.</p> <p>You can read more about action selector attributes in <a href="http://erraticdev.blogspot.com/2010/12/using-custom-action-method-selector.html" rel="nofollow">my blog post</a> but basically you'd have to write something similar to this:</p> <pre><code>public class UserAccountActivatedAttribute : ActionMethodSelectorAttribute { public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) { if (controllerContext == null) { throw new ArgumentNullException("controllerContext"); } // get profile id first int id = int.Parse(controllerContext.RouteData.Values["id"] ?? -1); bool isActivated = // some code to get this state return isActivated; } } </code></pre> <p>And that's basically it.</p> <p>This will make it possible for users to access their profile regardless whether their account has been activated or not. Or maybe even deactivated at some later time... And it will work seamlessly in the background.</p> <h2>One important advantage</h2> <p>If you'd have two actions with different names (as Juraj suggests), one for active profiles and other for activation, you'd have to do the checking in both, because even active users would be able to access activation action:</p> <pre><code>profile/4 &gt; for active profiles profile/activate/4 &gt; for inactive profiles </code></pre> <p>Both actions should be checking state and redirect to each other in case that state doesn't "fit". This also means that each time a redirection would occur, profile will get checked twice. In each action.</p> <p>Action method selector will check profiles <strong>only once</strong>. No matter what state user profile is in.</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