MVC Interview Questions and Answers


1.What does MVC represent in ASP.NET? or What is MVC ?
- 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.What is the difference between ASP.NET MVC and ASP.NET WebForms?
-ASP.NET WebForms uses the page controller patterns to render a layout. Whereas, ASP.NET MVC provides a model that doesn’t have connection with the View so it becomes easier to test and maintain the applications.

-ASP.NET WebForms uses the Front controller pattern for all the pages to process the web applications requests and used to facilitate routing architecture. Whereas, ASP.NET MVC has the View that is called before the controller and this controller is used to render the View that is based on the actions as the user interacts with the interface.

-ASP.NET WebForms manage the state of the model by using the view state and server based controls. Whereas, ASP.NET MVC doesn’t manage the state information like WebForms.

-ASP.NET WebForms are event driven. Whereas, ASP.NET MVC are test driven. 
5.How ASP.NET MVC differs from ASP.NET Web forms?

-ASP.NET MVC does not use View State to record state information whereas ASP.NET web form uses View State to record state information.

-ASP.NET Web forms are better for Rapid application development in the case if you small development team. ASP.NET MVC is better for large project where you give more priority to testability and maintainability.

-In ASP.NET Web form View is called after controller wherein ASP.NET MVC the rendering of View is managed by controller based on the action.

-ASP.NET Web forms development is easy to learn compared to ASP.NET MVC development.

6.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. 

7.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. 

8.What is the Request flow used for ASP.NET MVC framework?
Request flow handles the request from the clients and passes it to the server. The Request flow is as follows:

-Request is being taken from User to controller.

-Controller processes the request from the user and creates a data Model of that particular request.

-Data model that is being created is then passed to View that handles the frontend or the design.

-View then transforms the Data Model by using its own functions in an appropriate output format.

-The output format that is being given by the View is then gets rendered to the Browser and the View will be seen by the user. 

9.What is the procedure to create the environment for ASP.NET?
The creations of ASP.NET environment requires 6 steps and these are as follows:

1. User sends a request to the server. The server checks the request and also tries to locate the API extension that can serve the request. The request is processed depending on the file extension.

2. In case of the first request there is a class called ApplicationManager gets created and it also creates the application domain where the website can run.

3. The application that is created creates a hosting environment using HttpRuntime object. With these the core components like HttpContext, HttpRequest and HttpResponse also gets created.

4. These objects that are created provide the response to the request.

5. The object that is assigned to the process page is called as HttpApplication.

6.This process starts the processing of the request that is handled by the Http module events. 

10.What is the procedure to handle the process request using MHPM events fired?

The procedure to handle the process request using MHPM events fired is as follows:

-After the creation of HttpApplication the request is handled using three different sections namely HttpModule, Page, and HttpHandler.

-Invoking of different events take place this is according to the customize logic that is defined by the developer.

-HttpModule and HttpHandlers are used to inject the custom logic before and after the page are being processed. HttpHandlers are used to handle the extension based requests.

-HttpHandlers are the main part of the ASPX and it handles the logic that is being given in the processor. 
What is the flow of processing of the request?


The steps used by MHPM are as follows:

1. Client requests the server for processing. ASP.NET engine at that time create an event that is used to inject the customized logic. There are different types of events that can be used like BeginRequest, AuthenticateRequest, AuthorizeRequest, etc.

2. When the events are fired then the engine invoke additional processes like ProcessRequest that is handled by HttpHandler and this only implements it to handle the request.

3. HttpHandler executes the business logic that creates the page object. During the creation of the mage many events are fired that helps in writing the custom logic inside the page events. The events are as follows: Init, Load, Validate, Event, Render and Unload.

4. HttpModule provides the post page execution i.e. when the page object is executed and unloaded from memory. The events that are required during post execution is as follows: PostRequestHandlerExecute, ReleaserequestState, UpdateRequestCache and EndRequest.

11.What are the features that make ASP.NET more used framework?
-ASP.NET consists of many classes that can be used to build the software applications

-ASP.NET provides inbuilt functionality that allows the programmers to code in an efficient and easy way.

-ASP.NET uses classes for working with the file system, accessing a database, using regular expressions, and generating images.

-ASP.NET is used with the Microsoft framework that is used to build web applications.

-It implements the classes through which the web page caching, authentication and authorization can be handled.

-ASP.NET provides an easy interface to build web application using the framework and make it easier for programmer to program. 

12.What are the ASP.NET MVC folder conventions?
ASP.NET MVC is a framework that focuses on convention over the configuration. The folder structure is very essential for every framework to see:

-App_Data: this contains the database files like local instance SQL server express database files.

-Content: it contains the static content that is used for the web application building like CSS, images, etc.

-Controller: it contains the controller classes that are used in ASP.NET MVC architecture.

-Models: it consists of the model classes that are used to interact with the content and go in sync with the MVC architecture of the ASP.NET.

-Scripts: JavaScript files that include the AJAX library and jQuery script files.

-Views: it contains the frontend view that will be seen after the model is rendered. 

13.What is the function of new view engine in ASP.NET?
-New View Engine is the pluggable modules that is implemented with templates and provide syntax options.

-New View Engine uses master file templates of ASP.NET Web Forms that became popular and includes Spark and NHaml.

-New View Engine uses the HTML generation template approach to optimize the complete work with the Razor engine.

-Razor is used as a helper that includes all the helper methods and programming model features to synchronize with the .ASPX view engine.

-Multiple views are also popular using a single application or the site as the template can be viewed using a single engine or more flexibility can be given to the use of template. 

14.What is the main function of Razor in ASP.NET
-Razor is a view engine that allows the static HTML or the content to be started with the server and then the content is made dynamic by adding the server code to it.

-Razor is designed to make the process of coding flexible and provide standardization.

-Razor also provides an easy way to integrate the server code into the HTML markup with few keystrokes.

-Razor is used a view engine that is very expressive in writing style. The coding becomes simpler due to the support libraries.

-Razor supports many local functions with other functionalities that help in block reading and writing. It also has pre-defined set of functions that can be used to make the coding more easier. 
15. What is Razor?

-When we need to include our c# or VB code with the HTML markup within ASP.NET Web pages then sometimes it becomes very tedious. To overcome this problem, Razor was implemented.

-Razor was introduced with ASP.NET MVC 3 to make ease of writing C# or VB code in HTML pages.

-There is no need to use ASP.NET delimiter <%= %>. We can use only “@” character to achieve the same goal.

-For example
<%= DateTime.Now %>
Can be written in Razor simply as
@DateTime.Now

-In razor all syntax starts with “@” and if there is only one line of code then there is no need to use “{“.

-The extension of Razor file is cshtml for C# work environment and vbhtml for Visual Basic work environment.
-New View Engine is the pluggable modules that is implemented with templates and provide syntax options.

-New View Engine uses master file templates of ASP.NET Web Forms that became popular and includes Spark and NHaml.

-New View Engine uses the HTML generation template approach to optimize the complete work with the Razor engine.

-Razor is used as a helper that includes all the helper methods and programming model features to synchronize with the .ASPX view engine.

-Multiple views are also popular using a single application or the site as the template can be viewed using a single engine or more flexibility can be given to the use of template. 

16.Write a program using code nuggets to create a simple application?


-To create a program using ASP.NET code nuggets should be written using the syntax as follows:

-<%= %>: this is for the blocks and it indicates code nuggets that are used within the HTML markup:

<h1> Code Nudget</h1>
<h2> This <%=name %>, is the only reason <%= DateTime.Now.Year %> </h2>
<p> Checkout <a href= “/Products/<%=productId %> this product </a> </p>

-Code nuggets is useful when displaying of the characters as they act as references to start and stop the code sequence and used in an easy manner. 

17.Write a program using Razor view engine to create a simple application?

-Razor view engine is an expressive language that is used with ASP.NET MVC.

-Razor uses @character and doesn’t need the code block to be explicitly written like code nuggets.
<h1> Razor </h1>
<h2> This @name, is very useful and it shows @DateTime.Now.Year </h2>
<p> Checkout <a href= “Product/@productId”> the product</a>

-Razor uses the semantic parser to use it within the code blocks. It identifies the statements as a self contained code blocks and implicitly closes it.

-Razor is more easily available and use as it allows fast coding to be written without using complex syntaxes. 

18.What are the namespace classes used in ASP.NET MVC?

ASP.NET MVC uses the namespace classes that as follows:

-System.Web.Mvc namespace: this consists of classes and interfaces that follows MVC pattern to create web applications. This includes the controllers, libraries, actions, views, models.

-System.Web.Mvc.Ajax namespace: this consists of classes that support the AJAX scripts and used in the web applications. This also include the AJAX related settings and options.

-System.Web.Mvc.Async namespace: this consists of classes and interfaces that provide asynchronous actions in the web applications.

-System.Web.Mvc.Html namespace: this consists of classes in the form of helper application and follows the MVC pattern. This includes the forms, controls, links, views and validations. 

19.Which are the namespaces used in ASP.NET MVC?

-All the namespaces and classes used for ASP.NET MVC reside in the System.Web.Mvc assembly.

-System.Web.Mvc namespace

This namespace provides classes and interfaces that support the MVC pattern for ASP.NET Web applications. This namespace also contains classes that manage controllers, controller factories, partial views, action results, views and model binders.

-System.Web.Mvc.Ajax namespace

This namespace provides classes that support Ajax scripts in an ASP.NET MVC application. The namespace also provides support of Ajax scripts and Ajax option settings.

-System.Web.Mvc.Async namespace

This namespace provides classes and interfaces that support asynchronous actions in an ASP.NET MVC application.

-System.Web.Mvc.Html namespace

This namespace provides classes that help in rendering HTML controls in an MVC application. The namespace contains classes providing forms, input controls, links, partial views, and validation support.

20.What is Repository Pattern in ASP.NET MVC?
-Repository pattern is used as a default entity operation that allow the decoupling of the components used for presentation.

-Repository pattern allows easy testing in the form of unit testing and mocking.

-Repository pattern will have the proper infrastructure services to be used in the web applications.

-It uses the mechanism of encapsulating that provides storage, retrieval and query for the implementation of the repository.

-Repository patterns are hard coded in the application that is to be used in ASP.NET MVC architecture.

21.What is repository pattern in MVC.NET?

-Repository pattern is useful for decoupling entity operations form presentation, which allows easy mocking and unit testing.

-The Repository will delegate to the appropriate infrastructure services to get the job done. Encapsulating in the mechanisms of storage, retrieval and query is the most basic feature of a Repository implementation.

-Repository pattern is useful for decoupling entity operations form presentation, which allows easy mocking and unit testing.

-Most common queries should also be hard coded to the Repositories as methods. Which MVC.NET to implement repository pattern Controller would have 2 constructors on parameterless for framework to call, and the second one which takes repository as an input:

class myController: Controller
{
private IMyRepository repository;

// overloaded constructor
public myController(IMyRepository repository)
{
this.repository = repository;
}

// default constructor for framework to call
public myController()
{
//concreate implementation
myController(new someRepository());
}
public ActionResult Load()
{
// loading data from repository
var myData = repository.Load();
}
}

22.What is difference between MVC (Model-View-Controller) and MVP (Model-View-Presenter)?


-MVC has the controller handling the important work and all the request from the applications are coming directly to the controller. Whereas, MVP has the View as the handler and it handles the requests that are being generated.

-MVC is used for web application to provide an easy interface through which the interaction can be done. Whereas, MVP is having the View as its first object that gets executed in the pipeline and it is responsible for passing the event to the Presenter.

-ASP.NET Webforms are hard to implement in MVC structure due to its complex architecture and giving the total control to the controller to handle the requests. Whereas, MVP divides the roles such, that it becomes easy to handle the requests and pass it from one model to another. 


23.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.

24.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.


25.Write a program to call the js function when the change is being made in the Dropdown list made in ASP.NET MVC?


- To call the function of the js (JavaScript) in the dropdown list in ASP.NET MVC the function has to be written like:
<script type="text/javascript">
function selectedIndexChanged() { }
</script>

- The function need to be called using the code nuggets or if the Razor view engine the the js has to be integrated as:

<%:Html.DropDownListFor(x => x.SelectProduct, new SelectList(Model.Products, "Value", "Text"), "Select product", new { id = "dpDown", onchange="selectedIndexChanged()" })%>

What is the main function of URL routing system in ASP.NET MVC?


-URL routing system provides flexibility to the system and it also enables to define new URL mapping rules that can be used with web applications.

-URL routing system is used to map the application and its routing information gets passed to right controller and action method.

-URL routing system processes and executes the method to run the application without using many designed rules.

-It is used to construct the outgoing URLs that can be used to handle the actions that have the ability to map both incoming and outgoing URLs that adds more flexibility to the application code.

-It follows the rules to execute the application globally and handle the logic that is required for the application.

26.What is significance of routing?

-ASP.NET MVC uses ASP.NET routing, to map incoming browser requests to controller action methods.

-In ASP.NET Routing mechanism a routing table is maintained which is created when the web application first starts.

-The route table is present in the Global.asax file.

-In routing mechanism the physical path of page will not be used in URL and therefore hiding the application file system hierarchy from outer world.

-By using the routing mechanism the URL search becomes more user friendly.

No comments:

Post a Comment