Controller is the base class from which we create Controllers in ASP.NET MVC (article). The base class Controller has many useful properties and methods. If you look in to the MSDN documentation of the Controller class you can see a comprehensive list of that.
Controller actions
HttpContext (type = HttpContextBase)
Gets HTTP specific information about an individual HTTP request. You should note that the type of HttpContext is HttpContextBase. See difference between HttpContext and HttpContextBase.
HttpContextBase class is an abstract class that contains same members as the HttpContext class. But this class enables you to create derived classes that are like HttpContext class, but you can customize and that work outside ASP.NET pipeline.
HttpContext.Current: static property which returns current HttpContext for the thread. You can also use Page.Context property to access HttpContext for the current HTTP request.
HttpContext.Current: static property which returns current HttpContext for the thread. You can also use Page.Context property to access HttpContext for the current HTTP request.
Request (type = HttpRequestBase)
Gets the HttpRequestBase object for the current HTTP request. Enables you to read HTTP values sent by a client during a web request. This is an abstract class which contains same members as the HttpRequest class. Same as in HttpContext, HttpRequestBase class allows you to create derived classes that are like HttpRequest class.
HttpRequestWrapper class derives from the HttpRequestBase class. The HttpRequestWrapper class serves as a wrapper for HttpRequest class. At run time, you typically use and instance of the HttpRequestWrapper class to invoke members of the HttpRequest.
Response (type = HttpResponseBase)
Same as in Request
RouteData (type = RouteData)
Gets the route data for the current request
Server (type = HttpServerUtilityBase)
Gets the HttpServerUtility object that provides methods that are used during Web request processing
Session (type = HttpSessionStateBase)
The implementation of HttpSessionStateBase class, HttpSessionState provides access to session-state values as well as session-level settings and lifetime management methods. You can access session and functionality through the Session property of the current HttpContext, or Session property of the Page.
You might wonder why there's a Session property in Controller as well as in HttpContext.Current. They are almost the same. The Session property in Page class actually calls the HttpContext.Current.Session itself. But there are places where you can't directly call Session property, such as from static context like WebMethod.
See the difference between HttpContext.Current.Session and Session. also look at these stackoverflow references (link2, link3, link4 also the google search)
See the difference between HttpContext.Current.Session and Session. also look at these stackoverflow references (link2, link3, link4 also the google search)
User (type = IPrincipal)
Gets the user security information for the current HTTP request
Steps
Additional note: GenericIdentity objects
You can use GenericIdentity class in conjunction with the GenericPrincipal class to create an authorization scheme that exists independent of a Windows domain.Steps
- Create a new instance of the identity class and initialize it with the username.
- Create a new instance of GenericPrincipal class and initialize it with previously created GenericIdentity and array of string of user roles you want to attach
- Attach the principal to the current thread.
In case you want to store additional data in generic principal then create a class out of GenericIdentity class.
Can I Overload controller methods?
You can do something like below (Sources - SO)
Can I Overload controller methods?
You can do something like below (Sources - SO)
[ActionName("MyOverloadedName")]
Good article with useful information. Looking forward to more articles like this. Keep up the good work
ReplyDelete