<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1005796411192369842</id><updated>2011-10-17T03:44:16.686-07:00</updated><title type='text'>Imran Hussein</title><subtitle type='html'>Code, family and everything in between.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://imranhussein.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1005796411192369842/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://imranhussein.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Imran Hussain</name><uri>http://www.blogger.com/profile/16382618214226144006</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1005796411192369842.post-9186824098515433244</id><published>2010-08-10T09:01:00.000-07:00</published><updated>2011-01-14T22:35:12.400-08:00</updated><title type='text'>A Tale of ASP.NET MVC Controller</title><content type='html'>&lt;b&gt;What is Controllers?&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Controller is a public class which consist of public methods called action. When&lt;br /&gt;a request is made, route engine finds controller based on requested URL and an action method get executed. &lt;br /&gt;Each ASP.NET MVC request is directed at executing a method on a selected controller&lt;br /&gt;class. The controller’s method (actions) runs, processes input data, executes some application logic, and figures out the view to use. Controller job is to pass data to View for presentation.&lt;br /&gt;&lt;br /&gt;For example:&lt;br /&gt;&lt;i&gt;public class DefaultController: Controller&lt;br /&gt;{&lt;br /&gt;public ActionResult Home()&lt;br /&gt;{&lt;br /&gt;return Content("Hello World");&lt;br /&gt;}&lt;br /&gt;}&lt;/i&gt;&lt;br /&gt;Note:&lt;br /&gt;A controller’s method is expected to return an ActionResult object or any object that inherits from ActionResult.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Controller Methods and Input Parameters:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The input data is any data posted with the HTTP request. In action method defination, you can certainly access any available input through the familiar Request object and any of its child collections, such as Form, Cookies, ServerVariables, and QueryString.&lt;br /&gt;&lt;br /&gt;However you have 3 options to access data 1. Using request object 2. Model Binding 3. Auto data/ parameter mapping (i.e. If you place parameters list in action method, asp.net mvc will try to fill data using parameter collection. You need to add same parameter names which you use in view. It is case insensitive. So xyz and XYZ no problem.)&lt;br /&gt;&lt;br /&gt;&lt;b&gt;More about Controllers:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Developers writing a controller class are simply required to define a public class with a few public methods. &lt;br /&gt;This controller class, however, must derive from a mandatory base class—the Controller class. &lt;br /&gt;In turn, the Controller class derives from a base class that implements a given interface.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Controller structure&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;As we know now, the primary responsibility of a controller is executing any task associated with the incoming&lt;br /&gt;request. Now, your controller class is derived from Controller class which is derived from ControllerBaseClass which implement IController interface.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;IController Interface&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The IController interface has a precise, single responsibility: executing the specified request context. &lt;br /&gt;A request context is the ASP.NET MVC abstraction that encapsulates information about the HTTP request that matches a defined route.&lt;br /&gt;&lt;i&gt;public interface IController&lt;/i&gt;&lt;br /&gt;&lt;i&gt;{&lt;/i&gt;&lt;br /&gt;&lt;i&gt;void Execute(RequestContext requestContext);&lt;/i&gt;&lt;br /&gt;&lt;i&gt;}&lt;/i&gt;&lt;br /&gt;and RequestContext is,&lt;br /&gt;&lt;i&gt;public class RequestContext&lt;/i&gt;&lt;br /&gt;&lt;i&gt;{&lt;/i&gt;&lt;br /&gt;&lt;i&gt;public RequestContext(HttpContextBase httpContext, RouteData routeData);&lt;/i&gt;&lt;br /&gt;&lt;i&gt;public HttpContextBase HttpContext { get; internal set; }&lt;/i&gt;&lt;br /&gt;&lt;i&gt;public RouteData RouteData { get; internal set; }&lt;/i&gt;&lt;br /&gt;&lt;i&gt;}&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;Important: You should note the use of the ASP.NET MVC HttpContextBase class instead of the ASP.NET native HttpContext class. This is done to decouple the controller from the ASP.NET infrastructure for testing purposes. Essentially, HttpContextBase serves as the base class for classes that contain HTTP-specific information about an individual HTTP request.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;The ControllerBase Class&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The class ControllerBase represents the base class for all ASP.NET MVC controllers and it implemented IController. This is abstract class which means you can only inherit this. It has many properties and methods which are implemented in Controller class. This is because incase you want to write your own controller.&lt;br /&gt;&lt;br /&gt;&lt;i&gt;public abstract class ControllerBase : IController&lt;/i&gt;&lt;br /&gt;&lt;i&gt;{&lt;/i&gt;&lt;br /&gt;&lt;i&gt;// Methods&lt;/i&gt;&lt;br /&gt;&lt;i&gt;protected ControllerBase();&lt;/i&gt;&lt;br /&gt;&lt;i&gt;protected virtual void Execute(RequestContext requestContext);&lt;/i&gt;&lt;br /&gt;&lt;i&gt;protected abstract void ExecuteCore();&lt;/i&gt;&lt;br /&gt;&lt;i&gt;protected virtual void Initialize(RequestContext requestContext);&lt;/i&gt;&lt;br /&gt;&lt;i&gt;void IController.Execute(RequestContext requestContext);&lt;/i&gt;&lt;br /&gt;&lt;i&gt;// Properties&lt;/i&gt;&lt;br /&gt;&lt;i&gt;public ControllerContext ControllerContext { get; set; }&lt;/i&gt;&lt;br /&gt;&lt;i&gt;public TempDataDictionary TempData { get; set; }&lt;/i&gt;&lt;br /&gt;&lt;i&gt;public bool ValidateRequest { get; set; }&lt;/i&gt;&lt;br /&gt;&lt;i&gt;public IDictionary&lt;/i&gt;&lt;string, valueproviderresult=""&gt;&lt;i&gt; ValueProvider { get; set; }&lt;br /&gt;public ViewDataDictionary ViewData { get; set; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/i&gt; &lt;b&gt;Properties of the ControllerBase class&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&lt;/string,&gt;&lt;br /&gt;&lt;string, valueproviderresult=""&gt; ControllerContext&lt;br /&gt;Gets and sets an object that encapsulates the operational context of the controller. The controller context consists of the request context plus a reference to the controller itself.&lt;/string,&gt;&lt;br /&gt;&lt;string, valueproviderresult=""&gt;&lt;br /&gt;&lt;br /&gt;TempData&lt;br /&gt;Gets and sets a dictionary of data that persists across only two successive requests. Any data stored in the dictionary is accessible in the context of the next request, but it is then automatically discarded.&lt;br /&gt;&amp;nbsp;&lt;/string,&gt;&lt;br /&gt;&lt;br /&gt;&lt;string, valueproviderresult=""&gt;ValidateRequest&lt;br /&gt;Indicates whether the request is valid. The constructor of the class sets it to True. The property is read/write.&lt;br /&gt;&lt;br /&gt;ValueProvider&lt;br /&gt;Gets and sets the parameters dictionary, which is a collection of values available to the controller that include the following, in this order: form values, route values, and query string values.&lt;br /&gt;&lt;br /&gt;ViewData&lt;br /&gt;Gets and sets a dictionary of values that the view object will receive to produce a new user interface following the controller’s action.&lt;br /&gt;&lt;br /&gt;the IController implementation:&lt;br /&gt;&lt;i&gt;void IController.Execute(RequestContext requestContext)&lt;br /&gt;{&lt;br /&gt;this.Execute(requestContext);&lt;br /&gt;}&lt;br /&gt;protected virtual void Execute(RequestContext requestContext)&lt;br /&gt;{&lt;br /&gt;if (requestContext == null)&lt;br /&gt;{&lt;br /&gt;throw new ArgumentNullException("requestContext");&lt;br /&gt;}&lt;br /&gt;this.VerifyExecuteCalledOnce();&lt;br /&gt;this.Initialize(requestContext);&lt;br /&gt;this.ExecuteCore();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;The ControllerContext class has ref to ongoing HTTP request,&lt;br /&gt;public class ControllerContext&lt;br /&gt;{&lt;br /&gt;// Fields&lt;br /&gt;. . .&lt;br /&gt;// Methods&lt;br /&gt;public ControllerContext();&lt;br /&gt;protected ControllerContext(ControllerContext controllerContext);&lt;br /&gt;public ControllerContext(RequestContext requestContext, ControllerBase controller);&lt;br /&gt;public ControllerContext(HttpContextBase httpContext, RouteData routeData,&lt;br /&gt;ControllerBase controller);&lt;br /&gt;// Properties&lt;br /&gt;public virtual ControllerBase Controller { get; set; }&lt;br /&gt;public virtual HttpContextBase HttpContext { get; set; }&lt;br /&gt;public RequestContext RequestContext { get; set; }&lt;br /&gt;public virtual RouteData RouteData { get; set; }&lt;br /&gt;// Properties available only in ASP.NET MVC 2&lt;br /&gt;public bool IsChildAction { get; }&lt;br /&gt;public ViewContext ParentActionViewContext { get; }&lt;br /&gt;}&lt;/i&gt; &lt;br /&gt;&lt;br /&gt;&lt;b&gt; Finally, Controller Class&lt;/b&gt;&lt;br /&gt;The Controller class inherits from ControllerBase and adds a bunch of new methods and properties.&lt;br /&gt;public abstract class Controller : ControllerBase,IActionFilter,IAuthorizationFilter,IDisposable,IExceptionFilter,IResultFilter&lt;br /&gt;{&lt;br /&gt;. . .&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Controller class Methods:&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Content&lt;br /&gt;Internal and overloaded method. It gets some raw data (primitive data, custom objects) and returns a ContentResult object to render it to the browser.&lt;br /&gt;&lt;br /&gt;CreateActionInvoker&lt;br /&gt;Virtual method. It creates an action invoker to be used to govern the execution of action requests.&lt;br /&gt;&lt;br /&gt;CreateTempDataProvider&lt;br /&gt;Virtual method. It creates the actual container for data accessible through the &lt;br /&gt;TempData dictionary. By default, the temp data provider is an instance of the SessionStateTempDataProvider class.&lt;br /&gt;&lt;br /&gt;Dispose&lt;br /&gt;Virtual method. It performs application-specific tasks associated with freeing, releasing, or resetting unmanaged resources used by the controller.&lt;br /&gt;&lt;br /&gt;ExecuteCore&lt;br /&gt;Takes care of executing the action method as specified in the route data associated with the current request.&lt;br /&gt;&lt;br /&gt;File&lt;br /&gt;Internal and overloaded method. It returns a FileResult object used to render the content of a file. Content to render can be expressed in a variety of formats: file name, byte array, or stream.&lt;br /&gt;&lt;br /&gt;HandleUnknownAction&lt;br /&gt;Virtual method. It is called whenever a request matches the controller, but not an action method of the controller. The default implementation just throws an exception. &lt;br /&gt;&lt;br /&gt;Initialize &lt;br /&gt;Performs another step of initialization on the controller class. It&amp;nbsp;first calls the base Initialize method (described earlier) and then instantiates a helper object for URL manipulation.&lt;br /&gt;&lt;br /&gt;JavaScript&lt;br /&gt;Internal and overloaded method. It returns a JavaScriptResult object that encapsulates a piece of script code to be written to the response stream.&lt;br /&gt;&lt;br /&gt;Json&lt;br /&gt;Internal and overloaded method. It returns a JsonResult object that encapsulates a JSON string resulting from the serialization of a&amp;nbsp;given object.&lt;br /&gt;&lt;br /&gt;PartialView&lt;br /&gt;Internal and overloaded method. It gets a view name and returns a&amp;nbsp;PartialViewResult object that renders a partial (that is, incomplete) view to the response stream. A partial view is much like a user control in Web Forms.&lt;br /&gt;&lt;br /&gt;Redirect&lt;br /&gt;Virtual method. It returns a RedirectResult object that contains information about the URL to redirect to.&lt;br /&gt;&lt;br /&gt;RedirectToAction&lt;br /&gt;Internal and overloaded method. It gets the controller name, action name, and route values. The method returns a&amp;nbsp;RedirectToRouteResult object to redirect to the URL identified by the specified controller, action, and route parameters.&lt;br /&gt;&lt;br /&gt;RedirectToRoute&lt;br /&gt;Internal and overloaded method. It gets route name and route values. The method returns a RedirectToRouteResult object to redirect to the URL identified by the specified route and related parameters.&lt;br /&gt;&lt;br /&gt;TryUpdateModel&lt;br /&gt;Internal and overloaded method. It updates the specified model instance using values currently stored in the parameters dictionary exposed via the ValueProvider property. The method returns a&amp;nbsp;Boolean value to indicate success or failure of the update.&lt;br /&gt;&lt;br /&gt;UpdateModel&lt;br /&gt;Internal and overloaded method. It works like TryUpdateModel except that it throws an exception if the update fails.&lt;br /&gt;&lt;br /&gt;View&lt;br /&gt;Internal and overloaded method. It returns a ViewResult object that renders a view (that is, a new page) to the response stream.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Controller class Properties&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;ActionInvoker&lt;br /&gt;Gets and sets an IActionInvoker object for the controller. An action invoker defines the contract for invoking an action in response to an&amp;nbsp;HTTP request. This object is responsible for the actual execution of the action.&lt;br /&gt;&lt;br /&gt;Binders&lt;br /&gt;Gets and sets the collection of model binders available for the application. A&amp;nbsp;model binder is a sort of serializer for complex types that need to be passed around across requests. (More on this later.)&lt;br /&gt;&lt;br /&gt;HttpContext&lt;br /&gt;Gets all HTTP-specific information about the ongoing request.&lt;br /&gt;&lt;br /&gt;ModelState&lt;br /&gt;Gets a ModelStateDictionary dictionary object that represents the current state of the model object. The model object, if defined for a view, is populated with posted data. The ModelState dictionary contains information about anything that is wrong with the posted values. The&amp;nbsp;property mirrors the ModelState property of the ViewData collection. Its primary use is to carry message errors to the view after the action method executed and validated posted data.&lt;br /&gt;&lt;br /&gt;Request&lt;br /&gt;Gets the ASP.NET MVC abstraction of the ASP.NET native Request object. It returns an instance of the HttpRequestBase class.&lt;br /&gt;&lt;br /&gt;Response&lt;br /&gt;Gets the ASP.NET MVC abstraction of the ASP.NET native Response object. It returns an instance of the HttpResponseBase class.&lt;br /&gt;&lt;br /&gt;RouteCollection&lt;br /&gt;Internal property. Gets and sets the collection of routes for the application.&lt;br /&gt;&lt;br /&gt;RouteData&lt;br /&gt;Gets the RouteData object for the current request. The RouteData object encapsulates information about a route, such as tokens and the route handler. The RouteData class also offers methods to read tokens with ease.&lt;br /&gt;&lt;br /&gt;Server&lt;br /&gt;Gets the ASP.NET MVC abstraction of the ASP.NET native Server object. It&amp;nbsp;returns an instance of the HttpServerUtilityBase class.&lt;br /&gt;&lt;br /&gt;Session&lt;br /&gt;Gets the ASP.NET MVC abstraction of the ASP.NET native Session object. It returns an instance of the HttpSessionStateBase class.&lt;br /&gt;&lt;br /&gt;TempDataProvider&lt;br /&gt;Gets and sets the ITempDataProvider object responsible for storing data for the next request. The default provider stores data in the session state. The class is named SessionStateTempDataProvider.&lt;br /&gt;&lt;br /&gt;Url&lt;br /&gt;Gets and sets the helper object used to generate URLs using specified ASP.NET routes. The helper object is of type System.Web.Mvc.UrlHelper.&lt;br /&gt;&lt;br /&gt;User&lt;br /&gt;Gets the ASP.NET MVC abstraction of the ASP.NET native User object. It&amp;nbsp;returns an object that implements the IPrincipal interface.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Execution of a Request&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Any requests that hit an ASP.NET MVC application are destined to be resolved with the invocation&lt;br /&gt;of an action method within a controller class. Defined on the Controller class, the ExecuteCore method is where the action method is actually invoked.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Summary&lt;/b&gt;&lt;br /&gt;The typical behavior of a controller can be summarized in four main steps: getting input&lt;br /&gt;data, executing the request-related action method, preparing data for the view, and triggering&lt;br /&gt;the refresh of the view.&lt;br /&gt;&lt;/string,&gt;&lt;br /&gt;&lt;string, valueproviderresult=""&gt; Reference: MSDN, ASP.NET MVC 2 by Dino Esposito&lt;/string,&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1005796411192369842-9186824098515433244?l=imranhussein.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://imranhussein.blogspot.com/feeds/9186824098515433244/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://imranhussein.blogspot.com/2010/08/tale-of-aspnet-mvc-controller-part-1-of.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1005796411192369842/posts/default/9186824098515433244'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1005796411192369842/posts/default/9186824098515433244'/><link rel='alternate' type='text/html' href='http://imranhussein.blogspot.com/2010/08/tale-of-aspnet-mvc-controller-part-1-of.html' title='A Tale of ASP.NET MVC Controller'/><author><name>Imran Hussain</name><uri>http://www.blogger.com/profile/16382618214226144006</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1005796411192369842.post-1696152514007237630</id><published>2010-07-20T09:47:00.000-07:00</published><updated>2010-07-20T09:57:01.356-07:00</updated><title type='text'>My First blog</title><content type='html'>I have been planning ages ago to start writing my own blog :) but I couldn't start due to not adding in my to-do list of each day.&lt;br /&gt;&lt;br /&gt;Finally, I have added blogging in my to-do list. So, now lets see how it goes.&lt;br /&gt;&lt;br /&gt;You shall find my blogs mainly about web development using Microsoft available stack of tools usually asp.net, asp.net mvc, entity framework, c#, jquery, css and html.&lt;br /&gt;&lt;br /&gt;I shall also write about my daily activities to share my personal opinion.&lt;br /&gt;&lt;br /&gt;Please let me know if you have any suggestion for my upcoming blogs.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1005796411192369842-1696152514007237630?l=imranhussein.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://imranhussein.blogspot.com/feeds/1696152514007237630/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://imranhussein.blogspot.com/2010/07/my-first-blog.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1005796411192369842/posts/default/1696152514007237630'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1005796411192369842/posts/default/1696152514007237630'/><link rel='alternate' type='text/html' href='http://imranhussein.blogspot.com/2010/07/my-first-blog.html' title='My First blog'/><author><name>Imran Hussain</name><uri>http://www.blogger.com/profile/16382618214226144006</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>
