MVC Interview Notes

MVC Interview Notes

Sanjeevi Subramani's photo
Sanjeevi Subramani

Published on Feb 9, 2021

7 min read

Subscribe to my newsletter and never miss my upcoming articles

MVC stands for Model, View and Controller. MVC separates application into three components — Model, View and Controller.

· Model: Model represents shape of the data and business logic. It maintains the data of the application. Model objects retrieve and store model state in a database.

o Model is a data and business logic.

· View: View is a user interface. View display data using model to the user and also enables them to modify the data.

o View is a User Interface.

· Controller: Controller handles the user request. Typically, user interact with View, which in-turn raises appropriate URL request, this request will be handled by a controller. The controller renders the appropriate view with the model data as a response.

a. ActionResult

ViewResult

Represents HTML and markup.

EmptyResult

Represents No response.

ContentResult

Represents string literal.

FileContentResult/FilePathResult/ FileStreamResult

Represents the content of a file

JavaScriptResult

Represent a JavaScript script.

JsonResult

Represent JSON that can be used in AJAX

RedirectResult

Represents a redirection to a new URL

RedirectToRouteResult

Represent another action of same or other controller

PartialViewResult

Returns HTML from Partial view

HttpUnauthorizedResult

Returns HTTP 403 status

b. Action verbs:

Http method

Usage

GET

To retrieve the information from the server. Parameters will be appended in the query string.

POST

To create a new resource.

PUT

To update an existing resource.

HEAD

Identical to GET except that server do not return message body.

OPTIONS

OPTIONS method represents a request for information about the communication options supported by web server.

DELETE

To delete an existing resource.

PATCH

To full or partial update the resource.

c. ViewData VS ViewBag VS TempData:

ViewData

ViewBag

TempData

It is Key-Value Dictionary collection

It is a type object

It is Key-Value Dictionary collection

ViewData is a dictionary object and it is property of ControllerBase class

ViewBag is Dynamic property of ControllerBase class.

TempData is a dictionary object and it is property of controllerBase class.

ViewData is Faster than ViewBag

ViewBag is slower than ViewData

NA

ViewData is introduced in MVC 1.0 and available in MVC 1.0 and above

ViewBag is introduced in MVC 3.0 and available in MVC 3.0 and above

TempData is also introduced in MVC1.0 and available in MVC 1.0 and above.

ViewData also works with .net framework 3.5 and above

ViewBag only works with .net framework 4.0 and above

TempData also works with .net framework 3.5 and above

Type Conversion code is required while enumerating

In depth, ViewBag is used dynamic, so there is no need to type conversion while enumerating.

Type Conversion code is required while enumerating

Its value becomes null if redirection has occurred.

Same as ViewData

TempData is used to pass data between two consecutive requests.

It lies only during the current request.

Same as ViewData

TempData only works during the current and subsequent request

In the case of the MVC internal DynamicViewDataDictionary class — it ultimately ends up binding to this:

public override bool TryGetMember(GetMemberBinder binder, out object result)

{

result = this.ViewData[binder.Name];

return true;

}

For var a = ViewBag.Foo

And

public override bool TrySetMember(SetMemberBinder binder, object value)

{

this.ViewData[binder.Name] = value;

return true;

}

For ViewBag.Foo = Bar;

In other words — the statements are effectively being rewritten to wrappers around the dictionary indexer.

Because of that, there’s certainly no way it could be faster than doing it yourself.

Tempdata — peek and keep

· Peek — reads it and mark it for non deletion

· Keep — mark it for non deletion.

· Just read tempdata it will mark it for deletion.

d. Web api

http protocol

controller — action — routing

(GET/POST/PUT/PATCH/DELETE)

Eg:

[HttpPost]

public void SaveNewValue([FromBody]string value)

{

}

Codes are grouped into five categories based upon the first number.

S. No.

HTTP Status Code

Description

1.

1XX

Informational

2.

2XX

Success

3.

3XX

Redirection

4.

4XX

Client-Side Error

5.

5XX

Server-Side Error

Table: HTTP Status Code with Description

Some of the commonly seen HTTP Status Codes are: 200 (Request is Ok), 201 (Created), 202 (Accepted), 204 (No Content), 301 (Moved Permanently), 400 (Bad Request), 401 (Unauthorized), 403 (Forbidden), 404 (Not Found), 500 (Internal Server Error), 502 (Bad Gateway), 503 (Service Unavailable) etc.

i. Difference between Web API and MVC controller

Web API Controller

MVC Controller

Derives from System.Web.Http.ApiController class

Derives from System.Web.Mvc.Controller class.

Method name must start with Http verbs otherwise apply http verbs attribute.

Must apply appropriate Http verbs attribute.

Specialized in returning data.

Specialized in rendering view.

Return data automatically formatted based on Accept-Type header attribute. Default to json or xml.

Returns ActionResult or any derived type.

Requires .NET 4.0 or above

Requires .NET 3.5 or above

ii. ASP.NET Web API vs WCF

Web API

WCF

Open source and ships with .NET framework.

Ships with .NET framework

Supports only HTTP protocol.

Supports HTTP, TCP, UDP and custom transport protocol.

Maps http verbs to methods

Uses attributes based programming model.

Uses routing and controller concept similar to ASP.NET MVC.

Uses Service, Operation and Data contracts.

Does not support Reliable Messaging and transaction.

Supports Reliable Messaging and Transactions.

Web API can be configured using HttpConfiguration class but not in web.config.

Uses web.config and attributes to configure a service.

Ideal for building RESTful services.

Supports RESTful services but with limitations.

 
Share this