Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7940421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:23:01+00:00 2026-06-03T23:23:01+00:00

I’m relatively new with MVC3, but I’m using it, C# and EF4 to create

  • 0

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:

    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" }
        );
    }

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:

http://www.mysite.com/profile/4

…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.

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

And what about people who make up (or have a typographical error) in their Id value, like:

http://www.mysite.com/profile/40000000000

(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.)

Any help would be greatly appreciated.

EDIT:

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?

    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 => q.ProfileId == (Convert.ToInt32(id))).FirstOrDefault();
            }
            base.OnActionExecuting(filterContext);
        }
    }


    Cannot implicitly convert type 'System.Linq.IQueryable<Mysite.Models.Profile>' to 'Mysite.Models.Profile'. An explicit conversion exists (are you missing a cast?)

EDIT #2:

I’m working on Robert’s suggestion, and have made partial progress. My code currently looks like this:

    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;
        }
    }

which I got to after reading the blog entry, and (believe it or not) this posting: http://pastebin.com/Ea09Gf4B

I needed to change ActionSelectorAttribute to ActionMethodSelectorAttribute in order to get things moving again.

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?

if (int.TryParse(filterContext.ActionParameters["Id"], id) && id != 0) {

    bool isActivated = db.Claimed.Where(c => c.ProfileId == id).FirstOrDefault();

EDIT #3:

Here is my current state of the 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 => q.ProfileId == id).FirstOrDefault();
            bool isActivated = profile;// some code to get this state 
            return isActivated;
        }
    }

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: Bind a routevalue to a property of an object that is part of viewmodel

The line

var profile = db.Profiles.Where(q => q.ProfileId == id).FirstOrDefault();

errors on the db. section, with error message as follows:

Cannot access a non-static member of outer type ‘MySite.Controllers.HomeController’ via nested type ‘MySite.Controllers.HomeController.UserAccountActivatedAttribute’

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:23:03+00:00Added an answer on June 3, 2026 at 11:23 pm

    Others have suggested many things already, but let me bring something else to the table here.

    Action Method Selector

    In order to keep your controller actions clean, you can write an action method selector attribute to create two simple actions:

    [ActionName("Index")]
    public ActionResult IndexNonActivated(int id)
    {
        ...
    }
    
    [ActionName("Index")]
    [UserAccountActivated]
    public ActionResult IndexActivated(int id)
    {
        ...
    }
    

    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.

    You can read more about action selector attributes in my blog post but basically you’d have to write something similar to this:

    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;
        }
    }
    

    And that’s basically it.

    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.

    One important advantage

    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:

    profile/4 > for active profiles
    profile/activate/4 > for inactive profiles
    

    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.

    Action method selector will check profiles only once. No matter what state user profile is in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.