Thursday, 21 August 2014

ASP .net MVC Faq-performance optimization

What is the difference between ViewData, ViewBag and TempData?
In order to pass data from controller to view and in next subsequent request, ASP.NET MVC framework provides different options i.e. ViewData, ViewBag and TempData.
Both ViewBag and ViewData are used to to communicate between controller and corresponding view.But this communication is only for server call, it becomes null if redirect occurs. So, in short, its a mechanism to maintain state between controller and corresponding view.
ViewData is a dictionary object while ViewBag is a dynamic property (a new C# 4.0 feature). ViewData being a dictionary object is accessible using strings as keys and also requires typecasting for complex types.On the other hand, ViewBag doesn’t have typecasting and null checks.
TempData is also a dictionary object that stays for the time of an HTTP Request. So, Tempdata can be used to maintain data between redirects i.e from one controller to the other controller.
You can easily find examples for implementation of ViewBag, ViewData and TempData here.


What are Action Filters in ASP.NET MVC?

If we need to apply some specific logic before or after action methods, we use action filters. We can apply these action filters to a controller or a specific controller action. Action filters are basically custom classes that provide a mean for adding pre-action or post-action behavior to controller actions.
For example,
§  Authorize filter can be used to restrict access to a specific user or a role.
§  OutputCache filter can cache the output of a controller action for a specific duration.
§ 

§  Which assembly is used to define the MVC framework and Why ?

§   
§ 
The MVC framework is defined through System.Web.Mvc assembly. 
This is because this is the only assembly which contains classes and interfaces that support the ASP.NET Model View Controller (MVC) framework for creating Web applications.
§ 

§  Does the unit testing of an MVC application is possible without running controllers in an ASP.NET process ?

§   
§ 
In an MVC application, all the features are based on interface. So, it is easy to unit test a MVC application. 
And it is to note that, in MVC application there is no need of running the controllers for unit testing.
§ 

§  Can we share a view across multiple controllers ?

§   
§ 
Yes, It is possible to share a view across multiple controllers by putting a view into the shared folder. 
By doing like this, you can automatically make the view available across multiple controllers.
§ 

§  Mention some of the return types of a controller action method ?

§   
§ 
An action method is used to return an instance of any class which is derived from ActionResult class. 
Some of the return types of a controller action method are: 
i) ViewResult : It is used to return a webpage from an action method 
ii) PartialViewResult : It is used to send a section of a view to be rendered inside another view. 
iii) JavaScriptResult : It is used to return JavaScript code which will be executed in the user’s browser. 
iv) RedirectResult : Based on a URL, It is used to redirect to another controller and action method. 
v) ContentResult : It is an HTTP content type may be of text/plain. It is used to return a custom content type as a result of the action method. 
vi) JsonResult : It is used to return a message which is formatted as JSON. 
vii) FileResult : It is used to send binary output as the response. 
viii) EmptyResult : It returns nothing as the result.
§ 

§  Explain about 'page lifecycle' of ASP.NET MVC ?

§   
§ 
The page lifecycle of an ASP.NET MVC page is explained as follows: 

i) App Initialisation 
In this stage, the aplication starts up by running Global.asax’s Application_Start() method. 
In this method, you can add Route objects to the static RouteTable.Routes collection. 
If you’re implementing a custom IControllerFactory, you can set this as the active controller factory by assigning it to the System.Web.Mvc.ControllerFactory.Instance property. 

ii) Routing 
Routing is a stand-alone component that matches incoming requests to IHttpHandlers by URL pattern. 
MvcHandler is, itself, an IHttpHandler, which acts as a kind of proxy to other IHttpHandlers configured in the Routes table. 

iii) Instantiate and Execute Controller 
At this stage, the active IControllerFactory supplies an IController instance. 

iv) Locate and invoke controller action 
At this stage, the controller invokes its relevant action method, which after further processing, calls RenderView(). 

v) Instantiate and render view 
At this stage, the IViewFactory supplies an IView, which pushes response data to the IHttpResponse object.



No comments:

Post a Comment

Angular Tutorial (Update to Angular 7)

As Angular 7 has just been released a few days ago. This tutorial is updated to show you how to create an Angular 7 project and the new fe...