LightBlog

Thursday, 13 October 2016

ASP.NET MVC INTERVIEW QUESTIONS




These are the “MVC” frequently Common Questions in all companies
1)What is difference between mvc3, mvc4 and mvc5?
2)Difference between View Bag ,View Data and Temp Data ?

ViewData

1.       ViewData is a dictionary object that is derived from ViewDataDictionary class.
1.  public ViewDataDictionary ViewData { get; set; }
2.       ViewData is a property of ControllerBase class.
3.       ViewData is used to pass data from controller to corresponding view.
4.       It’s life lies only during the current request.
5.       If redirection occurs then it’s value becomes null.
6.       It’s required typecasting for getting data and check for null values to avoid error.
7.     ViewData is faster than ViewBag.

ViewBag

1.       ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
2.       Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
1.  public Object ViewBag { get; }
3.       ViewBag is a property of ControllerBase class.
4.       It’s life also lies only during the current request.
5.       If redirection occurs then it’s value becomes null.
6.       It doesn’t required typecasting for getting data.

TempData

1.       TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.
1.  public TempDataDictionary TempData { get; set; }
2.       TempData is a property of ControllerBase class.
3.       TempData is used to pass data from current request to subsequent request (means redirecting from one page to another).
4.       It’s life is very short and lies only till the target view is fully loaded.
5.       It’s required typecasting for getting data and check for null values to avoid error.
6.       It is used to store only one time messages like error messages, validation messages. To persist data with TempData refer this article: Persisting Data with TempData

How can we do validations in MVC?

One of the easiest ways of doing validation in MVC is by using data annotations. Data annotations are nothing but attributes which can be applied on model properties. For example, in the below code snippet we have a simple Customer class with a property customercode.
This CustomerCode property is tagged with a Required data annotation attribute. In other words if this model is not provided customer code, it will not accept it.
public class Customer
{
    [Required(ErrorMessage="Customer code is required")]
    public string CustomerCode
    {
        set;
        get;
    } 

3)which IDE is used for your project and what is the difference between 2010 ,2012,2013 and 2015?
4)What is Routing?
5)What is attribute routing?
6) What are the filters in mvc?
7) How to register filters in mvc?
8) What is code First Approach?
9) Why should we give dbcontextname and connection string name same in code first approach?
10) Difference Between code first and database first approach ?
11) What is entity frame work and what are the types?
12) Which version you use entityframe work in your project?
13) What is bundling?
14) What are the Action filter types?
15) What is Linq and write a Query to linq with where condition as well as in and not in conditions?
16) What is repository?
17) If you have 1000 of records how to update those records in mvc?
18) What is peek and seek methods in temp-data?
19) How to send a data from controller view?
20) How to send a data view to controller?
21) How to maintain session in mvc?
22) What is webapi?
23) What are the resources in webapi?
24) How to call webapi to ui?
25) What is authentication and authorization in mvc?
26) What is the difference between soap and rest?
27) ACID properties?
28) What is mvc flow?
today interviewer asked How to implement multiple inheritance in MVC C#?
==========================================================================
C#interview Questions
=================
1)what is the difference between interface and abstract class?
2)what is the default access specifier in interface?
3)what is explicit interface?
4)what is late bind and early binding?
5)what is garbage collector?
6)what is finalize and dispose ()?
7)what is assembly?
8)difference between string and string builder?
9)difference between array and array list?
10)what is singleton?
11)what is partial class?
13)what is anonymous type?
14)what is virtual method ?
15)what is constructor and types?
==========================================================================
project related
====================
1) what is your current project and can you explain project architecture?
2)how many modules are in your project?
3)how many screens ?
4)which version you used in u r project?

What is MVC Flow
Request Flow handles the request from the user ( client ) and send it to the server. Lets look into details on the function flow internally when client raises a request. This is what happens when u type an asp .net mvc application URL into the address bar of a web browser & clicks on enter.
Request -->Routing --> Handler -->Controller --> Action --> View --> Response


Detailed Look at the Request Flow


1.    Request comes into ASP.NET
2.    ASP.NET Routing finds the route match by calling RouteCollection.GetRouteData
3.    This in turn calls RouteBase.GetRouteData on each route until it finds a match
4.    The IRouteHandler for the matching route has its GetHttpHandler method called
5.    The MvcHandler runs (ProcessRequest is called)
6.    The MVC controller factory locates and creates the controller in CreateController
7.    The ControllerActionInvoker determines which action to run in InvokeAction
8.    The AuthorizationFilter stage executes (this includes the authorization method on the controller itself)
9.    The ActionExecuting stage executes
10.   The requested action method is executed
11.   The ActionExecuted stage executes
12.   If there is a result object then the ResultExecuting stage executes
13.   If the result wasn't cancelled then the ActionResult's ExecuteResult method is executed
14.   The ResultExecuted stage executes
15.   If an error occured then the Exception stage executes


What is ACID test
ACID are set of properties that guarantee that database transactions are processed reliably.
Let’s talk about every property in detail
1) Atomicity
It says, Database transaction should complete 100% or zero. Example – let’s say there is a transaction which has two steps. Let say one of them complete and power will fail. It is must that even step 1 should rollback or else it is said that Atomicity is violated.
2) Consistency
This property ensures that database transaction will bring database from one valid state to another.
It means only valid data will be written to database. Valid data means which won’t violate any constraints and business rules.

3) Isolation
Every Transaction should be independent of each of other.
It means if two transactions are trying to access the same data and are trying to execute at the same time then one should wait until other ends or else it said that Database is violating Isolation.
4) Durability
Changes need to persist permanently once transaction is termed as successful.
Let say a transaction has 2 steps to complete and both of them executes and completes. Now it time to commit changes to real database. Let say even after success, final commit it not made and waiting for something else to happen (like waiting for memory to get cleaned) and in the meantime power fails. It will make us lost all the changes. Thus we say transaction was not durable. To make it durable we should commit it as soon as we realize that it completes.


How to maintain session in mvc?

Using ASP.NET MVC TempData and Session to pass values across Requests
) How to send a data from controller view?
By Post Reques and Get Request

What is the use of Keep and Peek in "TempData"?

Once "TempData" is read in the current request it's not available in the subsequent request. If we want "TempData" to be read and also available in the subsequent request then after reading we need to call "Keep" method as shown in the code below.

@TempData["MyData"];
TempData.Keep("MyData");

The more shortcut way of achieving the same is by using "Peek". This function helps to read as well advices MVC to maintain "TempData" for the subsequent request.

string str = TempData.Peek("Td").ToString();


Repository(Unit testing and types of testing process)
Note There are many ways to implement the repository and unit of work patterns. You can use repository classes with or without a unit of work class. You can implement a single repository for all entity types, or one for each type. If you implement one for each type, you can use separate classes, a generic base class and derived classes, or an abstract base class and derived classes. You can include business logic in your repository or restrict it to data access logic. You can also build an abstraction layer into your database context class by using IDbSet interfaces there instead of DbSet types for your entity sets. The approach to implementing an abstraction layer shown in this tutorial is one option for you to consider, not a recommendation for all scenarios and environments.

Q.What is Bundling and Minification in Mvc

Bundling: It’s a simple logical group of files that could be referenced by unique name and being loaded with one HTTP requestor.

Minification: It’s a process of removing unnecessary whitespace, line break and comments from code to reduce its size thereby improving load times.Top of Form 1

Asp.Net MVC is a lightweight and highly testable open source framework for building highly scalable and well designed web applications. Here is the list of released version history of ASP.NET MVC Framework with theirs features.
Asp.Net MVC1
1.       Released on Mar 13, 2009
2.       Runs on .Net 3.5 and with Visual Studio 2008 & Visual Studio 2008 SP1
3.       MVC Pattern architecture with WebForm Engine
4.       Html Helpers
5.       Ajax helpers
6.       Routing
7.       Unit Testing
Asp.Net MVC2
1.       Released on Mar 10, 2010
2.       Runs on .Net 3.5, 4.0 and with Visual Studio 2008 & 2010
3.       Strongly typed HTML helpers means lambda expression based Html Helpers
4.       Templated Helpers
5.       Support for Data Annotations Attribute
6.       Client-side validation
7.       UI helpers with automatic scaffolding & customizable templates
8.       Attribute-based model validation on both client and server
9.       Overriding the HTTP Method Verb including GET, PUT, POST, and DELETE
10.   Areas for partitioning a large applications into modules
11.   Asynchronous controllers
Asp.Net MVC3
1.       Released on Jan 13, 2011
2.       Runs on .Net 4.0 and with Visual Studio 2010
  • The Razor view engine
3.       Improved Support for Data Annotations
4.       Remote Validation
5.       Compare Attribute
6.       Sessionless Controller
7.       Child Action Output Caching
8.       Dependency Resolver
9.       Entity Framework Code First support
10.   Partial-page output caching
11.   ViewBag dynamic property for passing data from controller to view
12.   Global Action Filters
13.   Better JavaScript support with unobtrusive JavaScript, jQuery Validation, and JSON binding
14.   Use of NuGet to deliver software and manage dependencies throughout the platform
15.   Good Intellisense support for Razor into Visual Studio
Asp.Net MVC4
1.       Released on Aug 15, 2012
2.       Runs on .Net 4.0, 4.5 and with Visual Studio 2010SP1 & Visual Studio 2012
3.      ASP.NET Web API
4.       Enhancements to default project templates
5.      Mobile project template using jQuery Mobile
6.       Display Modes
7.      Task support for Asynchronous Controllers
8.       Bundling and minification
9.      Support for the Windows Azure SDK
Asp.Net MVC5
1.       Released on 17 October 2013
2.       Runs on .Net 4.5, 4.5.1 and with Visual Studio 2013
3.       One Asp.Net
4.       Asp.Net Identity
5.       ASP.NET Scaffolding
6.       Authentication filters - run prior to authorization filters in the ASP.NET MVC pipeline
7.       Bootstrap in the MVC template
8.       ASP.NET Web API2
Comparing the Model First, Database First and Code First approaches
So when should one choose one method over the others? Here is a simple comparison of the advantages all three approaches.
Model First:
·         Good support with EDMX designer
·         We can visually create the database model
·         EF generates the Code and database script
·         Extensible through partial classes
·         We can modify the model and update the generated database.
Database First:
·         An existing database can be used
·         Code can be auto-generated.
·         Extensible using partial classes/ T4 templates
·         The developer can update the database manually
·         There is a very good designer, which sync with the underlining database
Code First:
·         There is full control of the model from the Code;
·         no EDMX/designer
·         No manual intervention to DB is required
·         The database is used for data only



Questions and answers

How can we maintain session in MVC?
Sessions can be maintained in MVC by 3 ways tempdata , viewdata and viewbag.
Difference Between tempdata , viewdata and viewbag.
Temp data: -Helps to maintain data when you move from one controller to other controller or from one
action to other action. In other words when you redirect,“tempdata” helps to maintain data between those
redirects. It internally uses session variables.
View data: - Helps to maintain data when you move from controller to view.
View Bag: - It’s a dynamic wrapper around view data. When you use “Viewbag” type casting is not
required. It uses the dynamic keyword internally.
Session variables: - By using session variables we can maintain data from any entity to any entity.
Session variables: - By using session variables we can maintain data from any entity to any entity.
Hidden fields and HTML controls: - Helps to maintain data from UI to controller only. So you can send
data from HTML controls or hidden fields to the controller using POST or GET HTTP methods.
Below is a summary table which shows different mechanism of persistence.
Maintains data between ViewData/ViewBag TempData Hidden fields Session
Controller to Controller No Yes No Yes
Controller to View Yes No No Yes
View to Controller No No Yes Yes
What is life of “TempData” ?
“TempData” is available for the current request and in the subsequent request it’s available depending on
whether “TempData” is read or not.
So if “TempData” is once read it will not be available in the subsequent request.
What is the use of Keep and Peek in “TempData”?
Once “TempData” is read in the current request it’s not available in the subsequent request. If we want
“TempData” to be read and also available in the subsequent request then after reading we need to call
“Keep” method as shown in the code below.
@TempData[“MyData”];
TempData.Keep(“MyData”);
The more shortcut way of achieving the same is by using “Peek”. This function helps to read as well
advices MVC to maintain “TempData” for the subsequent request.
string str = TempData.Peek("Td").ToString();
What are partial views in MVC?
Partial view is a reusable view (like a user control) which can be embedded inside other view.
For example let’s say all your pages of your
How can we do validations in MVC?
One of the easy ways of doing validation in MVC is by using data annotations. Data annotations are
nothing but attributes which you can be applied on the model properties

public class Customer
{
[Required(ErrorMessage="Customer code is required")]
What are partial views in MVC?
Partial view is a reusable view (like a user control) which can be embedded inside other view.
For example let’s say all your pages of your site have a standard structure with left menu, header
and footer as shown in the image below.
Figure 6.5:- partial views in MVC
For every page you would like to reuse the left menu, header and footer controls. So you can go
and create partial views for each of these items and then you call that partial view in the main
view.
How did you create partial view and consume the same?
When you add a view to your project you need to check the “Create partial view” check box.
What is razor in MVC?
It’s a light weight view engine. Till MVC we had only one view type i.e.ASPX, Razor was introduced in
MVC 3.
Why razor when we already had ASPX?
Razor is clean, lightweight and syntaxes are easy as compared to ASPX. For example in ASPX to display
simple time we need to write.
<%=DateTime.Now%>
In Razor it’s just one line of code.
@DateTime.Now
Explain the difference between layout and master pages ?
Layout are like master pages in ASP.NET Web form. Master pages give a standard look and feel for Web
form views while layout gives standard look and feel or acts like a template for razor views.
How to implement Ajax in MVC?
You can implement Ajax in two ways in MVC:-
Ajax libraries
Jquery
                   “Ajax.BeginForm” syntax.
This form calls a controller action called as “getCustomer”. So now the submit action click will
be an asynchronous ajax call.



No comments:

Post a Comment

LightBlog