Viva - MVC Question

MVC Question
Beginner:
1.       Explain MVC pattern?
The Model-View-Controller (MVC) pattern is an architectural design principle that separates an application into three main components: the model, the view, and the controller.
What is a Model?
It is basically a business entity which is used to represent the application data.
Often model objects retrieve data (and store data) from a database.
1.      MVC model is basically a C# or VB.NET class
2.      A model is accessible by both controller and view
3.      A model can be used to pass data from Controller to view
4.      A view can use model to display data in page.
5.      Often model objects retrieve data (and store data) from a database.
What is a View?
it's the presentation layer of ASP.Net MVC.
Most often the views are created from the model data.
1.      View is an ASPX page without having a code behind file
2.      All page specific HTML generation and formatting can be done inside view
3.      One can use Inline code (server tags ) to develop dynamic pages
4.      A request to view (ASPX page) can be made only from a controller’s action method
What is a Controller?
The Controller is responsible for taking the end user request and loading the appropriate Model and View.
Typically controllers read data from a view, control user input, and send input data to the model.
1.      Controller is basically a C# or VB.NET class which inherits system.mvc.controller
2.      Controller is a heart of the entire MVC architecture
3.      Inside Controller’s class action methods can be implemented which are responsible for responding to browser OR calling views.
4.      Controller can access and use model class to pass data to views
5.      Controller uses ViewData to pass any data to view

2.       What are the advantages of ASP.NET MVC?
                                It is difficult to unit test UI with webforms, where views in mvc can be very easily unit tested.

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.
3.       What is Controller in ASP.NET MVC?
 
4.       What is View in ASP.NET MVC?

5.       What is model in ASP.NET MVC?

6.       What is Razor View Engine?
       Razor view engine in asp.net mvc is syntax that allows you to write server side code on view. It helps to combine code and html in fluid manner.

The Razor syntax is a template markup syntax, based on the C# programming language, that enbles the programmer to use an HTML construction workflow. Instead of using the ASP.NET .ASPX markup syntax with <%= %> symbols to indicate code blocks, Razor syntax starts code blocks with a @ character and does not require explicit closing of the code-block.
7.       What are the 2 popular asp.net mvc view engines?
1 Razor
2. aspx
8.       What symbol would you use to denote, the start of a code block in razor views?
                           @
9.       In razor syntax, what is the escape sequence character for @ symbol?
                       The escape sequence character for @ symbol, is another @ symbol
10.   Give an example for Authorization filters in an asp.net mvc application?
1. RequireHttpsAttribute
2. AuthorizeAttribute
11.   What type of filter does OutputCacheAttribute class represents?
                             Result Filter
12.   What are the file extensions for razor views?
1. .cshtml - If the programming lanugaue is C#
2. .vbhtml - If the programming lanugaue is VB
13.   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 *@
14.   What is the namespace of asp.net mvc?
System.Web.Mvc
15.   What is ASP.NET MVC?
ASP.NET MVC is a free, fully supported Microsoft framework for developing great web applications using the Model-View-Controller pattern. It provides total control over your HTML and URLs, enables rich Ajax integration, and facilitates test driven development.
16.   What is difference between ViewBag and ViewData in ASP.NET MVC?
ASP.NET MVC offers us three options ViewData, VieBag and TempData for passing data from controller to view and in next request. ViewData and ViewBag are almost similar and TempData performs additional responsibility. Lets discuss or get key points on those three objects:
Similarities between ViewBag & ViewData :
  1. Helps to maintain data when you move from controller to view.
  2. Used to pass data from controller to corresponding view.
  3. Short life means value becomes null when redirection occurs. This is because their goal is to provide a way to communicate between controllers and views. It’s a communication mechanism within the server call.
Difference between ViewBag & ViewData:
  1. ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys.
  2. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
  3. ViewData requires typecasting for complex data type and check for null values to avoid error.
  4. ViewBag doesn’t require typecasting for complex data type.
ViewBag & ViewData Example:

public ActionResult Index()
{
    ViewBag.Name = "Monjurul Habib";
    return View();
}

public ActionResult Index()
{
    ViewData["Name"] = "Monjurul Habib";
    return View();
} 
In View:

@ViewBag.Name 
@ViewData["Name"] 

17.   What is Routing in ASP.NET MVC?
ASP.NET routing enables you to use 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
18.   How to identify AJAX request with C# in MVC.NET?
19.   What are the 3 main components of an ASP.NET MVC application?

20.   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.
How does the 'page lifecycle' of ASP.Net MVC works?
Below are the processed followed in the sequence -

1. App initialize What is Separation of Concerns in ASP.NET ASP.Net MVC ation
2. Routing
3. Instantiate and execute controller
4. Locate and invoke controller action
5. Instantiate and render view. 

 Intermediate:
21.   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.
22.   What is the main function of URL routing system in ASP.NET MVC? 
-URL routing system is used to map the application and its routing information gets passed to right controller and action method.

23.   What are the namespace classes used in ASP.NET MVC?
-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.
24.   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.

Advance:
25.   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
26.   What is the use of action filters in an MVC application?
·         Action filters are attribute
·         This could be applied on a particular Action
·         This could be applied on a Controller.
·         This modifies the way Action executes.
·         Custom Action filter could also be created and applied.
·         an easy way to apply an action filter to every action in an MVC application. All you need to do is register the filters during application startup:
protected void Application_Start()
{
    ...
    GlobalFilters.Filters.Add(new HandleErrorAttribute());
    GlobalFilters.Filters.Add(new FooFilter());
    GlobalFilters.Filters.Add(new BarFilter());
    ...
}


27.   What are the 2 ways of adding constraints to a route?
1. Use regular expressions
2.Use an object that implements IRouteConstraint interface
28.   What are three segments of the default route, which 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://dotnetcodes.com/Article/id/5
Controller Name = Article
Action Method Name = id
Parameter Id = 5
29.   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.
30.   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.

31.   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.
32.   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.
33.   In which assembly is the MVC framework defined?
                        System.Web.Mvc
34.   How to call JavaScript function on the change of dropdown list in ASP.Net MVC?


35.   How route table created 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.
36.   How do you avoid XSS vulnerabilities in ASP.Net (MVC)?
To avoid xss vulnerabilities, you have to use the syntax as '<%: %>' in ASP.NET MVC instead of using the syntax as '<%= %>' in .net framework 4.0.
This is because it does the HTML encoding.

Example:
<input type="text" value="<%: value%>" />


37.   What is repository pattern in MVC.NET?


http://www.codeproject.com/Articles/556995/ASP-NET-MVC-interview-questions-with-answers

http://www.codeproject.com/Articles/763928/MVC-Unit-Testing-Unleashed

http://codextreame.blogspot.com/2014/01/top-20-aspnet-mvc-interview-questions.html

Razor View Engine VS Web Form(ASPX) View Engine
Razor View Engine
Web Form View Engine

The namespace for Razor Engine is System.Web.Razor.
The namespace for Webform Engine is System.Web.Mvc.WebFormViewEngine.
The file extensions used with Razor Engine are different from Web Form Engine. It has .cshtml (Razor with C#) or .vbhtml (Razor with VB) extension for views, partial views, editor templates and for layout pages.
The file extensions used with Web Form Engine are also like Asp.net Web Forms. It has .aspx extension for views, .ascx extension for partial views & editor templates and .master extension for layout/master pages.
Razor has new and advance syntax that are compact, expressive and reduces typing.
Web Form Engine has the same syntax like Asp.net Web Forms uses for .aspx pages.
Razor syntax are easy to learn and much clean than Web Form syntax. Razor uses @ symbol to make the code like as:
  1. @Html.ActionLink("SignUp", "SignUp")
Web Form syntax are borrowed from Asp.net Web Forms syntax that are mixed with html and sometimes make a view messy. Webform uses <% and %> delimiters to make the code like as:
  1. <%: Html.ActionLink("SignUp", "SignUp") %>


Razor Engine is little bit slow as compared to Webform Engine.
Web Form Engine is faster than Razor Engine.
Razor Engine, doesn't support design mode in visual studio means you cannot see your page look and feel.
Web Form engine support design mode in visual studio means you can see your page look and feel without running the application.
Razor Engine support TDD (Test Driven Development) since it is not depend on System.Web.UI.Page class.
Web Form Engine doesn't support TDD (Test Driven Development) since it depend on System.Web.UI.Pageclass which makes the testing complex.

Comments

Popular posts from this blog

Travel RESUME CV

PTE