Thursday, 15 August 2013

asp.net mvc - Using HttpContext in controller constructor -



asp.net mvc - Using HttpContext in controller constructor -

i trying set property in constructor af controller this:

public applicationusermanager usermanager { get; private set; } public accountcontroller() { usermanager = httpcontext.getowincontext().get<applicationusermanager>(""); }

but explained here:

http://stackoverflow.com/a/3432733/1204249

the httpcontext not available in constructor.

so how can set property can access in every actions of controller?

you can move code read-only property on controller (or base of operations controller if need available across entire application):

public class accountcontroller : controller { private applicationusermanager usermanager; public applicationusermanager usermanager { if (usermanager == null) { //only instantiate object 1 time per request usermanager = httpcontext.getowincontext().get<applicationusermanager>(""); } homecoming usermanager; } }

asp.net-mvc asp.net-identity-2

No comments:

Post a Comment