Monday, November 28, 2016

ASP.NET MVC interview question

1) What is MVC?
MVC is a pattern which is used to split the application's implementation logic into three components: models, views, and controllers.

2) What is MVC (Model View Controller)?
MVC is an architectural pattern which separates the representation and user interaction. It’s divided into three broader sections, Model, View, and Controller. Below is how each one of them handles the task.

The View is responsible for the look and feel.
Model represents the real world object and provides data to the View.
The Controller is responsible for taking the end user request and loading the appropriate Model and View.

3) What are the benefits of using MVC?
There are two big benefits of MVC:

Separation of concerns is achieved as we are moving the code-behind to a separate class file. By moving the binding code to a separate class file we can reuse the code to a great extent.
Automated UI testing is possible because now the behind code (UI interaction code) has moved to a simple .NET class. This gives us opportunity to write unit tests and automate manual testing.

4) Can you explain the page life cycle of MVC?
Below are the processed followed in the sequence -
App initialization
Routing
Instantiate and execute controller
Locate and invoke controller action
Instantiate and render view.

5) What are the advantages of MVC over ASP.NET?
Provides a clean separation of concerns among UI (Presentation layer), model (Transfer objects/Domain Objects/Entities) and Business Logic (Controller).
Easy to UNIT Test.
Improved reusability of model and views. We can have multiple views which can point to the same model and vice versa.
Improved structuring of the code.

6) What is the use of ViewModel in MVC?

ViewModel is a plain class with properties, which is used to bind it to strongly typed view. ViewModel can have the validation rules defined for its properties using data annotations.


7) What is Razor View Engine?
Razor is the first major update to render HTML in ASP.Net MVC 3. Razor was designed specifically for view engine syntax. Main focus of this would be to simplify and code-focused templating for HTML generation.


8) What are Actions in ASP.Net MVC?
Actions are the methods in Controller class which is responsible for returning the view or json data. Action will mainly have return type : "ActionResult" and it will be invoked from method : "InvokeAction()" called by controller.

9) How to enable Attribute Routing?
Just add @Model.CustomerName the method : "MapASP.Net MVCAttributeRoutes()" to enable attribute routing

10) Explain JSON Binding?
JavaScript Object Notation (JSON) binding support started from ASP.Net MVC3 onwards via the new JsonValueProviderFactory, which allows the action methods to accept and model-bind data in JSON format. This is useful in Ajax scenarios like client templates and data binding that need to post data back to the server.

11) which is a better fit, Razor or ASPX?
As per Microsoft, Razor is more preferred because it’s light weight and has simple syntaxes.

12) How to implement AJAX in MVC?
You can implement AJAX in two ways in MVC:

13) AJAX libraries
jQuery
Below is a simple sample of how to implement AJAX by using the “AJAX” helper library. In the below code you can see we have a simple form which is created by using the Ajax.BeginForm syntax. This form calls a controller action called getCustomer. So now the submit action click will be an asynchronous AJAX call.

Hide   Copy Code
<script language="javascript">
function OnSuccess(data1)
{
// Do something here
}
</script>


14) What are Code Blocks in Views?
Unlike code expressions that are evaluated and sent to the response, it is the blocks of code that are executed. This is useful for declaring variables which we may be required to be used later.

@{
 int x = 123;
 string y = "aa";
 }


15) What is RouteConfig.cs in ASP.Net MVC 4?
"RouteConfig.cs" holds the routing configuration for ASP.Net MVC. RouteConfig will be initialized on Application_Start event registered in Global.asax.

16) What is PartialView in ASP.Net MVC?
PartialView is similar to UserControls in traditional web forms. For re-usability purpose partial views are used. Since it's been shared with multiple views these are kept in shared folder. Partial Views can be rendered in following ways :

Html.Partial()
Html.RenderPartial()

17) How can we determine action invoked from HTTP GET or HTTP POST?
This can be done in following way : Use class : "HttpRequestBase" and use the method : "HttpMethod" to determine the action request type.

18) What is ViewData?

Viewdata contains the key, value pairs as dictionary and this is derived from class – “ViewDataDictionary“. In action method we are setting the value for viewdata and in view the value will be fetched by typecasting.


 19) How we can add the CSS in MVC?

Below is the sample code snippet to add css to razor views –

<link rel="StyleSheet" href="/@Href(~Content/Site.css")" type="text/css"/>

 20) Mention some action filters which are used regularly in MVC?

Below are some action filters used –

Authentication
Authorization
HandleError
OutputCache


21) Explain Keep method in Tempdata in MVC?

As explained above in case data in Tempdata has been read in current request only then “Keep” method has been used to make it available for the subsequent request.

@TempData[“TestData”];
TempData.Keep(“TestData”);

22) What is the use of the dispose method in C# ?

dispose method are used for forcefully garbage collection..!!!

23) Major and importance difference between for and foreach loop ?

a for loop is a construct that says "perform this operation n. times".a foreach loop is a construct that says "perform this operation against each value/object in this IEnumerable"

24) What is data dictionary?
Dictionaries provide fast lookups, based on keys, to get values. With them, we use keys and values of any type, including int and string.

No comments:

Post a Comment

Opps Part 1 : Abstraction

  Abstraction in C# is a fundamental concept of object-oriented programming (OOP) that allows developers t...