0
public class CheckoutController : Controller
{
    string userID;

    public CheckoutController()
    {
        userID = User.Identity.Name;
    }
    ...
}

When I run the above code, I get this error,

**Make sure that the controller has a parameterless public constructor.**

In that class, most of method need that userID, so I want to define that value in constructor, how can I solve this problem?

[Edit]

public class CheckoutController : Controller
{
    string userID;

    public CheckoutController()
    {
      //None
    }
}

This code works fine, no error.

4
  • 2
    Are you sure this is the controller that the routing is calling? It looks like you already have a parameterless constructor. Commented Apr 17, 2013 at 19:33
  • If so, have you rebuilt your code recently? It may be out of date... Commented Apr 17, 2013 at 19:33
  • @Expert as it is out of the box, only parameterless constructors work public CheckoutController(), but you would like to make use of public CheckoutController(int userId)? Commented Apr 17, 2013 at 20:21
  • There is nothing wrong with the code you've posted. Obviously, the problem is somewhere else, or you're working with old code. Commented Apr 17, 2013 at 20:38

1 Answer 1

3

Execution pipeline related values (Request, Response, and User) are binded ONLY AFTER the Controller's constructor method. That is why you can't use User.Identity as it is not binded yet. Only after the Step 3: IController.Execute() is when those contextual values are initialized.

http://blog.stevensanderson.com/blogfiles/2007/ASPNET-MVC-Pipeline/ASP.NET%20MVC%20Pipeline.jpg

Updated Poster: link to a newer poster based on @mystere-man's feedback thanks to @SgtPooki. But I am keeping the older embeddable image in here to make it a bit easier to reference.

ASP.NET MVC Pipeline

User.Identity.Name does not negatively affect the performance as it has already been decrypted from the FormsAuthentication cookie by the ASP.NET runtime (assuming you are using FormsAuthentication for your web application).

So don't bother caching it to a class member variable.

public class CheckoutController : Controller
{
    public CheckoutController() { /* leave it as is */ }

    public ActionResult Index()
    {
        // just use it like this
        string userName = User.Identity.Name;

        return View();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

FYI, that chart is WAY out of date, I think it was for a Pre-CTP1 of MVC1 and things changed quite a bit afterwards. There's a much better one referenced here blog.stevensanderson.com/2009/10/08/…
Thanks go to both of you. Also, I submitted an edit in order to provide a better link. And just in-case: i.imgur.com/96jp1eu.png

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.