Skip to main content

MVC5 Interview Questions?



MVC means ?

MVC stands for Model View Controller.
It divides an application into 3 component roles which is based on a framework methodology.
These component roles are discussed briefly as follows:

Models : These component roles are used to maintain the state which is persisted inside the Database.

Example: we might have a Product class that is used to represent order data from the Products table inside SQL.

Views : These component roles are used to display the user interface of the application, where this UI is created off of the model data.

Example: we might create an Product “Edit” view that surfaces textboxes, dropdowns and checkboxes based on the current state of a Product object.

Controllers : These component roles are used for various purposes like handling end user interaction, manipulating the model, and ultimately choosing a view to render to display UI.

In a MVC application, the views are used only for displaying the information whereas the controllers are used for handling and responding to user input and interaction.

What are the advantages of using asp.net mvc ?

The main advantages of using asp.net mvc are as follows:

i) One of the main advantage is that it will be easier to manage the complexity as the application is divided into model,view and controller.
ii) It gives us the full control over the behavior of an application as it does not use view state or server-based forms.
iii) It provides better support for test-driven development (TDD).
iv) You can design the application with a rich routing infrastructure as it uses a Front Controller pattern that processes Web application requests through a single controller.

What is the use of a controller in an MVC application ?

A controller will decide what to do and what to display in the view. It works as follows:

i) A request will be received by the controller
ii) Basing on the request parameters, it will decide the requested activities
iii) Basing on the request parameters, it will delegates the tasks to be performed
iv) Then it will delegate the next view to be shown

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:
  •  ViewResult : It is used to return a webpage from an action method
  •  PartialViewResult : It is used to send a section of a view to be rendered inside another view.
  • JavaScriptResult : It is used to return JavaScript code which will be executed in the user’s browser.
  •  RedirectResult : Based on a URL, It is used to redirect to another controller and action method.
  •  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.
  •  JsonResult : It is used to return a message which is formatted as JSON.
  •  FileResult : It is used to send binary output as the response.
  •  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:

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

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

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

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

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


What is App_Start folder in ASP.NET MVC?

App_Start folder has been introduced in MVC4. It contains various configurations files like as BundleConfig.cs, FilterConfig.cs, RouteConfig.cs, WebApiConfig.cs for your application. All these settings are registered within Application_Start method of Global.asax.cs file.

  • AuthConfig.cs: Used to configuration security settings, including sites for OAuth login
  • BundleConfig.cs – This is used to create and register bundles for CSS and JS files. By default, various bundles are added in this files including jQuery, jQueryUI, jQuery validation, Modernizr, and Site CSS. 
  • FIlterConfig.cs – This is used to register global MVC filters like error filters, actions filters etc. By default it contains HandleErrorAttribute filter. 
  • RouteConfig.cs – This is used to register various route patterns for your ASP.NET MVC application. By default, one route is registered here named as Default Route. 
  •  WebApiConfig.cs – This is used to register various WEB API routes like as ASP.NET MVC, as well as set any additional WEB API configuration settings.

 Differnce between HTML.TextBox and HTML.TextBoxFor?

Html.TextBox is not strongly typed and it doesn't require a strongly typed view meaning that you can hardcode whatever name you want as first argument and provide it a value:

Html.TextBoxFor is requires a strongly typed view and uses the view model

Example:-
@Html.TextBox("Name")
@Html.TextBoxFor(m => m.Name)
both will produce
<input id="Name" name="Name" type="textbox" />


Comments

  1. Nice post . Thanks for the shearing valuable information. I really fiend this type blog. Special thanks to writer.
    Asp.net developers

    ReplyDelete

Post a Comment

Popular posts from this blog

Connected and disconnected architecture in ADO.Net with Example

Connected Architecture of ADO.NET The architecture of ADO.net, in which connection must be opened to access the data retrieved from database is called as connected architecture. Connected architecture was built on the classes connection, command, datareader and transaction.  Connected architecture is when you constantly make trips to the database for any CRUD (Create, Read, Update and Delete) operation you wish to do. This creates more traffic to the database but is normally much faster as you should be doing smaller transactions. Disconnected Architecture in ADO.NET The architecture of ADO.net in which data retrieved from database can be accessed even when connection to database was closed is called as disconnected architecture. Disconnected architecture of ADO.net was built on classes connection, dataadapter, commandbuilder and dataset and dataview. Disconnected architecture is a method of retrieving a recor

HTTPHandler and HTTPModule in ASP.NET

If you want to implement pre-processing logic before a request hits the IIS resources. For instance you would like to apply security mechanism, URL rewriting, filter something in the request, etc. ASP.NET has provided two types of interception HttpModule and HttpHandler .   The web server examines the file name extension of the requested file, and determines which ISAPI extension should handle the request. Then the request is passed to the appropriate ISAPI extension.  For Example When an .aspx page is requested it is passed to ASP.Net page handler. Then Application domain is created and after that different ASP.Net objects like Httpcontext, HttpRequest, HttpResponse. HTTPModule: -    It's just like a filter. The Modules are called before and after the handler executes . -    HTTP Modules are objects which also participate the pipeline but they work before and after the HTTP Handler does its job, and produce additional services within the pipeline -  

ASP.NET Page Life Cycle with example

In this article, we are going to discuss the different methods and order they are executed during the load of an .aspx web page. Methods Description Page_PreInit Before page Initialization Page_Init Page Initialization LoadViewState View State Loading LoadPostData Postback Data Processing Page_Load Page Loading RaisePostDataChangedEvent PostBack Change Notification RaisePostBackEvent PostBack Event Handling Page_PreRender Page Pre Rendering Phase SaveViewState View State Saving Page_Render Page Rendering Page_Unload Page Unloading PreInit : The entry point of the page life cycle is the pre-initialization phase called “PreInit”. You can dynamically set the values of master pages and themes in this event. You can also dynamically create controls in this event.  Init : This event fires after each control h