Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC3 using routes or using controller logic?
    text
    copied!<p>I'm relatively new with MVC3, but I'm using it, C# and EF4 to create an application website. The routing that I'm using is the same as in the default Microsoft project created when I selected MVC3 pattern, nothing special:</p> <pre><code> public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults new[] { "MySite.Controllers" } ); } </code></pre> <p>And everything is working fine there. We're using the default Membership Provider, and users also get an INT value that identifies their account. This lets them see their profile pretty easily with a simple routing like:</p> <p>www.mysite.com/profile/4</p> <p>...for example. However, the client has asked that a lot of accounts be pre-generated and distributed to selected users. I've worked up a way to run that through SQL Server and it works fine, all the accounts got created (about a thousand). Additionally, I've add a bit field ('Claimed') that can help identify whether one of these pre-generated accounts has been 'activated' by these users.</p> <p>My question is, when a user is given a link to come visit their (un-activated) account, should I use a test when doing the initial <strong>routing</strong> on that page to identify their account as un-claimed and send them somewhere else to finish entering details into their account? Or should I let them go to the same page as everyone else, and have something in the <strong>controller</strong> logic that identifies this record as un-claimed, and then send them to another page to finish entering details etc.? Is there a good reason for doing one over the other?</p> <p>And what about people who make up (or have a typographical error) in their Id value, like:</p> <p>www.mysite.com/profile/40000000000</p> <p>(and the site only has a thousand users so far), should that be handled similarly, or through different means entirely? (I.e., in one scenario we're identifying an existing account that is not yet claimed, and in another scenario we're having to figure out that the account doesn't even exist.)</p> <p>Any help would be greatly appreciated.</p> <p><strong>EDIT:</strong></p> <p>I'm trying to implement Soliah's suggested solution, and got stuck a bit on the fact that the if (id != 0) didn't like that the id might not be in an INT. I'm past that now, and attempting to figure out a way to do the check if valid portion, but possibly I have not solved the problem with the id not being treated as an INT? Something is definitely not right, even though I'm trying to convert it again during my database test for validity. Any ideas on why I'm getting the error below? What am I missing?</p> <pre><code> public class ValidProfileIdAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { var id = (Convert.ToInt32(filterContext.ActionParameters["Id"])); if (id != 0) { // Check if valid and behave accordingly here. Profile profile = db.Profiles.Where(q =&gt; q.ProfileId == (Convert.ToInt32(id))).FirstOrDefault(); } base.OnActionExecuting(filterContext); } } Cannot implicitly convert type 'System.Linq.IQueryable&lt;Mysite.Models.Profile&gt;' to 'Mysite.Models.Profile'. An explicit conversion exists (are you missing a cast?) </code></pre> <p><strong>EDIT #2:</strong></p> <p>I'm working on Robert's suggestion, and have made partial progress. My code currently looks like this:</p> <pre><code> public class UserAccountActivatedAttribute : ActionMethodSelectorAttribute { public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo) { if (controllerContext == null) { throw new ArgumentNullException("controllerContext"); } bool isActivated = // some code to get this state return isActivated; } } </code></pre> <p>which I got to after reading the blog entry, and (believe it or not) this posting: <a href="http://pastebin.com/Ea09Gf4B" rel="nofollow noreferrer">http://pastebin.com/Ea09Gf4B</a></p> <p>I needed to change ActionSelectorAttribute to ActionMethodSelectorAttribute in order to get things moving again.</p> <p>However, what I don't see how to do is to get the Id value into the bool isActivated test. My database has a view ('Claimed') which can give back a true/false value, depending on the user's profile Id that it is handed, but I don't see where to add the Id. Would something like what Soliah edited work?</p> <pre><code>if (int.TryParse(filterContext.ActionParameters["Id"], id) &amp;&amp; id != 0) { bool isActivated = db.Claimed.Where(c =&gt; c.ProfileId == id).FirstOrDefault(); </code></pre> <p><strong>EDIT #3:</strong></p> <p>Here is my current state of the code:</p> <pre><code> public class UserAccountActivatedAttribute : ActionMethodSelectorAttribute { public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo) { if (controllerContext == null) { throw new ArgumentNullException("controllerContext"); } // get profile id first int id = int.Parse((string)controllerContext.RouteData.Values["id"]); var profile = db.Profiles.Where(q =&gt; q.ProfileId == id).FirstOrDefault(); bool isActivated = profile;// some code to get this state return isActivated; } } </code></pre> <p>For me, I had to change things to int.Parse((string)controllerContext.RouteData.Values to get them to work, which they seem to do (to that point.) I discovered that formatting here: <a href="https://stackoverflow.com/questions/1075129/bind-a-routevalue-to-a-property-of-an-object-that-is-part-of-viewmodel">Bind a routevalue to a property of an object that is part of viewmodel</a></p> <p>The line</p> <pre><code>var profile = db.Profiles.Where(q =&gt; q.ProfileId == id).FirstOrDefault(); </code></pre> <p>errors on the db. section, with error message as follows:</p> <blockquote> <p>Cannot access a non-static member of outer type 'MySite.Controllers.HomeController' via nested type 'MySite.Controllers.HomeController.UserAccountActivatedAttribute'</p> </blockquote> <p>...which is something that I have diligently tried to figure out with MSDN and Stack, only to come up empty. Does this ring any bells?</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