Monday, 17 March 2014

Interview Q & A on MVC in ASP.NET(Part 1)

1.What does MVC represent in ASP.NET?
- MVC stands for Model-View-Controller pattern that represents an architectural pattern for software.

- This separates the components of a Web application and helps in decoupling the business logic.

- It gives more flexibility to overall architecture that allows the changes to be made to a
layer, without affecting the other.

-M represents the Model view that specifies a specific domain data.

-V represents the view of the user interface components used to display the Model data.

-C represents the Controller that handles the user interactions and events. It manipulates the updates that model reflect at every change of the state of an application.

2.Which are the advantages of ASP.NET MVC framework?
-MVC framework is divided in model, view and controller which help to manage complex application. This way it divides the application in input logic, business logic and UI logic.

-MVC framework does not use view state or server-based forms which eliminate the problem of load time delays of HTML pages.

- MVC support ASP.NET routing which provide better URL mapping. In ASP.NET routing URL can be very useful for Search Engine Optimization (SEO) and Representation State Transfer (REST).

-MVC Framework support better development of test-driven development (TDD) application.

-In MVC Framework Testing becomes very easier. Individual UI test is also possible.

3.What do Model, View and Controller represent in an MVC application?
-MVC framework is a development method in which application is divided in three components: Model, View and Controller

-Model: In model we have to write our business logic and all the methods for data manipulations (CRUD)

-View: This component is responsible for user interface of the application.

-Controller: Controller handles the request and sends the response. It sends the request to View as a response or to Model. It acts as a mediator between Model and View.

4.How is the ASP.NET MVC architecture different from others?
-ASP.NET MVC uses a complete Model-View-Controller architecture that combines the Controller and the View in a way that both meet the dependency of each other.

-The testing of the architecture can be done by instantiating a View and carrying out the unit tests without running the controllers through the complete cycle.

-The control of MVC on the output is complete and it renders the HTML in a very clean way.

-The architecture provides an orientation towards the standard compliant pages and control over the behavior of the applications.

-The knowledge of many programming language gets reduced and the model can become more abstract from lots of details that is provided for the ease of use.

5.Why it is useful to use MVC instead of WebForms?
-MVC allows the user to write less amount of code to build the web applications as lots of components are integrated to provide the flexibility in the program.

-Lot of data control options is being provided that can be used in ViewState.

-The application tasks are separated into components to make it easier for the programmers to write the applications but at the same time the amount of the code also increases.

-Main focus remains on the testability and maintainability of the projects that is being carried out in large projects.

-It is being used for the projects that require rapid application development.

6.What is the page lifecycle of an ASP.NET MVC? 
The page lifecycle of ASP.NET MVC is having the following process and it is as follows:

-App initialization: in this the initiation of the application takes place that allow the application to interact the server and start to run the components.

-Routing: in this the messages are routed to the server for making the delivery of the request easier.

-Instantiate and execute controller: in this way the controller handles the request and passes it on to display the output or replies to the request.

-Locate and invoke controller action: The actions allow the controller to be located correctly and it invokes the correct action that has to be taken on the applications.

-Instantiate and render view: this helps in view the result of the application that is being built.

7.Can you describe ASP.NET MVC Request Life Cycle?
Following are the steps that are executed in ASP.NET MVC Request Life Cycle.

1. Application first receives the request and looks up Route object in RouteTable collection. Then the RouteData object is created.

2. Then RequestContext instance is created.

3. MvcHandler and pass RequestContext to handler is created.

4. Then IControllerFactory from RequestContext is identified.

5. Then the object of class that implements ControllerBase is created.

6. MyController.Execute method is called.

7. Then ControllerActionInvoker finds the action to invoke on the controller and executes that action on the controller and by calling the model associated view returns.

8.What are the 3 main components of an ASP.NET MVC application?
1. M - Model
2. V - View
3. C - Controller

9.In which assembly is the MVC framework defined?
System.Web.Mvc

10.Is it possible to combine ASP.NET webforms and ASP.MVC and develop a single web application?
Yes, it is possible to combine ASP.NET webforms and ASP.MVC and develop a single web application.

11.What does Model, View and Controller represent in an MVC application?
Model: Model represents the application data domain. In short the applications business logic is contained with in the model.

View: Views represent the user interface, with which the end users interact. In short the all the user interface logic is contained with in the UI.

Controller: Controller is the component that responds to user actions. Based on the user actions, the respective controller, work with the model, and selects a view to render that displays the user interface. The user input logic is contained with in the controller.

12.What is the greatest advantage of using asp.net mvc over asp.net webforms?
It is difficult to unit test UI with webforms, where views in mvc can be very easily unit tested.

13.Which approach provides better support for test driven development - ASP.NET MVC or ASP.NET Webforms?
ASP.NET MVC

14.What are the advantages of ASP.NET MVC?
1. Extensive support for TDD. With asp.net MVC, views can also be very easily unit tested.
2. Complex applications can be easily managed
3. Seperation of concerns. Different aspects of the application can be divided into Model, View and Controller.
4. ASP.NET MVC views are light weight, as they donot use viewstate.

15.Is it possible to unit test an MVC application without running the controllers in an ASP.NET process?
Yes, all the features in an asp.net MVC application are interface based and hence mocking is much easier. So, we don't have to run the controllers in an ASP.NET process for unit testing.

16.Is it possible to share a view across multiple controllers?
Yes, put the view into the shared folder. This will automatically make the view available across multiple controllers.

17.What is the role of a controller in an MVC application?
The controller responds to user interactions, with the application, by selecting the action method to execute and alse selecting the view to render.

18.Where are the routing rules defined in an asp.net MVC application?
In Application_Start event in Global.asax

19.Name a few different return types of a controller action method?
The following are just a few return types of a controller action method. In general an action method can return an instance of a any class that derives from ActionResult class.
1. ViewResult
2. JavaScriptResult
3. RedirectResult
4. ContentResult
5. JsonResult

20.What is the significance of NonActionAttribute?
In general, all public methods of a controller class are treated as action methods. If you want prevent this default behaviour, just decorate the public method with NonActionAttribute.

21.What is the significance of ASP.NET routing?
ASP.NET MVC uses ASP.NET routing, to map incoming browser requests to controller action methods. ASP.NET Routing makes use of route table. Route table is created when your web application first starts. The route table is present in the Global.asax file.

22.What are the 3 segments of the default route, that is present in an ASP.NET MVC application?
1st Segment - Controller Name
2nd Segment - Action Method Name
3rd Segment - Parameter that is passed to the action method

Example: http://pragimtech.com/Customer/Details/5
Controller Name = Customer
Action Method Name = Details
Parameter Id = 5

23.ASP.NET MVC application, makes use of settings at 2 places for routing to work correctly. What are these 2 places?
1. Web.Config File : ASP.NET routing has to be enabled here.
2. Global.asax File : The Route table is created in the application Start event handler, of the Global.asax file.

24.What is the adavantage of using ASP.NET routing?
In an ASP.NET web application that does not make use of routing, an incoming browser request should map to a physical file. If the file does not exist, we get page not found error.

An ASP.NET web application that does make use of routing, makes use of URLs that do not have to map to specific files in a Web site. Because the URL does not have to map to a file, you can use URLs that are descriptive of the user's action and therefore are more easily understood by users.

25.What are the 3 things that are needed to specify a route?
1. URL Pattern - You can include placeholders in a URL pattern so that variable data can be passed to the request handler without requiring a query string.
2. Handler - The handler can be a physical file such as an .aspx file or a controller class.
3. Name for the Route - Name is optional.

26.Is the following route definition a valid route definition?
{controller}{action}/{id}
No, the above definition is not a valid route definition, because there is no literal value or delimiter between the placeholders. Therefore, routing cannot determine where to separate the value for the controller placeholder from the value for the action placeholder.

27.What is the use of the following default route?
{resource}.axd/{*pathInfo}
This route definition, prevent requests for the Web resource files such as WebResource.axd or ScriptResource.axd from being passed to a controller.

28.What is the difference between adding routes, to a webforms application and to an mvc application?
To add routes to a webforms application, we use MapPageRoute() method of the RouteCollection class, where as to add routes to an MVC application we use MapRoute() method.

29.How do you handle variable number of segments in a route definition?
Use a route with a catch-all parameter. An example is shown below. * is referred to as catch-all parameter.
controller/{action}/{*parametervalues}

30.What are the 2 ways of adding constraints to a route?
1. Use regular expressions
2. Use an object that implements IRouteConstraint interface

31.Give 2 examples for scenarios when routing is not applied?
1. A Physical File is Found that Matches the URL Pattern - This default behaviour can be overriden by setting the RouteExistingFiles property of the RouteCollection object to true.
2. Routing Is Explicitly Disabled for a URL Pattern - Use the RouteCollection.Ignore() method to prevent routing from handling certain requests.

32.What is the use of action filters in an MVC application?
Action Filters allow us to add pre-action and post-action behavior to controller action methods.

If I have multiple filters impleted, what is the order in which these filters get executed?
1. Authorization filters
2. Action filters
3. Response filters
4. Exception filters

33.What are the different types of filters, in an asp.net mvc application?
1. Authorization filters
2. Action filters
3. Result filters
4. Exception filters

34.Give an example for Authorization filters in an asp.net mvc application?
1. RequireHttpsAttribute
2. AuthorizeAttribute

35.Which filter executes first in an asp.net mvc application?
Authorization filter

36.What are the levels at which filters can be applied in an asp.net mvc application?
1. Action Method
2. Controller
3. Application
[b]Is it possible to create a custom filter?[/b]
Yes

37.What filters are executed in the end?
Exception Filters

Is it possible to cancel filter execution?
Yes

38.What type of filter does OutputCacheAttribute class represents?
Result Filter

39.What are the 2 popular asp.net mvc view engines?
1. Razor
2. .aspx

40.What symbol would you use to denote, the start of a code block in razor views?
@

41.What symbol would you use to denote, the start of a code block in aspx views?
<%= %>

42.In razor syntax, what is the escape sequence character for @ symbol?
The escape sequence character for @ symbol, is another @ symbol

43.When using razor views, do you have to take any special steps to proctect your asp.net mvc application from cross site scripting (XSS) attacks?
No, by default content emitted using a @ block is automatically HTML encoded to protect from cross site scripting (XSS) attacks.

44.When using aspx view engine, to have a consistent look and feel, across all pages of the application, we can make use of asp.net master pages. What is asp.net master pages equivalent, when using razor views?
To have a consistent look and feel when using razor views, we can make use of layout pages. Layout pages, reside in the shared folder, and are named as _Layout.cshtml

45.What are sections?
Layout pages, can define sections, which can then be overriden by specific views making use of the layout. Defining and overriding sections is optional.

46.What are the file extensions for razor views?
1. .cshtml - If the programming lanugaue is C#
2. .vbhtml - If the programming lanugaue is VB

47.How do you specify comments using razor syntax?
Razor syntax makes use of @* to indicate the begining of a comment and *@ to indicate the end. An example is shown below.
@* This is a Comment *@

49.What is MVC?
MVC is a framework methodology that divides an application’s implementation into three component roles: models, views, and controllers.
“Models” in a MVC based application are the components of the application that are responsible for maintaining state. Often this state is persisted inside a database (for example: we might have a Product class that is used to represent order data from the Products table inside SQL).

“Views” in a MVC based application are the components responsible for displaying the application’s user interface. Typically this UI is created off of the model data (for example: we might create an Product “Edit” view that surfaces textboxes, dropdowns and checkboxes based on the current state of a Product object).

“Controllers” in a MVC based application are the components responsible for handling end user interaction, manipulating the model, and ultimately choosing a view to render to display UI. In a MVC application the view is only about displaying information – it is the controller that handles and responds to user input and interaction. 

50. What is Razor View Engine?
Razor view engine is a new view engine created with ASP.Net MVC model using specially designed Razor parser to render the HTML out of dynamic server side code. It allows us to write Compact, Expressive, Clean and Fluid code with new syntaxes to include server side code in to HTML.

51. What is namespace of asp.net mvc?
ASP.NET MVC namespaces and classes are located in the System.Web.Mvc assembly.
System.Web.Mvc namespace
Contains classes and interfaces that support the MVC pattern for ASP.NET Web applications. This namespace includes classes that represent controllers, controller factories, action results, views, partial views, and model binders.
System.Web.Mvc.Ajax namespace
Contains classes that support Ajax scripts in an ASP.NET MVC application. The namespace includes support for Ajax scripts and Ajax option settings.
System.Web.Mvc.Async namespace
Contains classes and interfaces that support asynchronous actions in an ASP.NET MVC application
System.Web.Mvc.Html namespace
Contains classes that help render HTML controls in an MVC application. The namespace includes classes that support forms, input controls, links, partial views, and validation.

51. What is the ‘page lifecycle’ of an ASP.NET MVC?
Following process are performed by ASP.Net MVC page:
1) App initialization
2) Routing
3) Instantiate and execute controller
4) Locate and invoke controller action
5) Instantiate and render view

52. How route table is created in ASP.NET MVC?
When an MVC application first starts, the Application_Start() method is called. This method, in turn, calls the RegisterRoutes() method. The RegisterRoutes() method creates the route table.

53.How do you avoid XSS Vulnerabilities in ASP.NET MVC?
Use thesyntax in ASP.NET MVC instead of usingin .net framework 4.0.

54. If I have multiple filters implemented, what is the order in which these filters get executed?
1. Authorization filters
2. Action filters
3. Response filters
4. Exception filters

55. What is difference between Viewbag and Viewdata in ASP.NET MVC?
The basic difference between ViewData and ViewBag is that in ViewData instead creating dynamic properties we use properties of Model to transport the Model data in View and in ViewBag we can create dynamic properties without using Model data.
character for @ symbol, is another @ symbol

56. What is Routing?

A route is a URL pattern that is mapped to a handler. The handler can be a physical file, such as an .aspx file in a Web Forms application. Routing module is responsible for mapping incoming browser requests to particular MVC controller actions.

No comments:

Post a Comment