Thursday, 16 May 2013

ASP.Net MVC Interview Questions and Answers

About ASP.Net MVC



The ASP.Net MVC is the framework provided by Microsoft that lets you develop the applications that follows the principles of Model-View-Controller (MVC) design pattern. The .Net programmers new to MVC thinks that it is similar to WebForms Model (Normal ASP.Net), but it is far different from the WebForms programming. 
This article will tell you how to quick learn the basics of MVC along with some frequently asked interview questions and answers on ASP.Net MVC

1. What is ASP.Net MVC

The ASP.Net MVC is the framework provided by Microsoft to achieve     separation of concerns that leads to easy maintainability of the     application.
Model is supposed to handle data related activity
View deals with user interface related work
Controller is meant for managing the application flow by communicating between Model and View.

2. Why to use ASP.Net MVC

The strength of MVC (i.e. ASP.Net MVC) listed below will answer this question
  • MVC reduces the dependency between the components; this makes your code more testable.
  • MVC does not recommend use of server controls, hence the processing time required to generate HTML response is drastically reduced.
  • The integration of java script libraries like jQuery, Microsoft MVC becomes easy as compared to Webforms approach.

3. What do you mean by Razor

The Razor is the new View engine introduced in MVC 3.0.
The View engine is responsible for processing the view files [e.g. .aspx, .cshtml] in order to generate HTML response.
The previous versions of MVC were dependent on ASPX view engine. 

4. Can we use ASPX view engine in latest versions of MVC

Yes. The Recommended way is to prefer Razor View

5. What are the benefits of Razor View?

  •      The syntax for server side code is simplified
  •      The length of code is drastically reduced
  •      Razor syntax is easy to learn and reduces the complexity

6. What is the extension of Razor View file?

.cshtml (for c#) and .vbhtml (for vb)

7. How to create a Controller in MVC



Create a simple class and extend it from Controller class. The bare minimum requirement for a class to become a controller is to inherit it from ControllerBase is the class that is required to inherit to create the controller but Controller class inherits from ControllerBase.

8. How to create an Action method in MVC

Add a simple method inside a controller class with ActionResult return type.

9. How to access a view on the server   

The browser generates the request in which the information like Controller name, Action Name and Parameters are provided, when server receives this URL it resolves the Name of Controller and Action, after that it calls the specified action with provided parameters. Action normally does some processing and returns the ViewResult by specifying the view name (blank name searches according to naming conventions).

 

10. What is the default Form method (i.e. GET, POST) for an action method

GET. To change this you can add an action level attribute e.g [HttpPost]


11. What is a Filter in MVC?


When user (browser) sends a request to server an action method of a controller gets invoked; sometimes you may require executing a custom code before or after action method gets invoked, this custom code is called as Filter.


12. What are the different types of Filters in MVC?


a. Authorization filter

b. Action filter

c. Result filter

d. Exception filter

[Do not forget the order mentioned above as filters gets executed as per above mentioned sequence]



13. Explain the use of Filter with an example?


Suppose you are working on a MVC application where URL is sent in an encrypted format instead of a plain text, once encrypted URL is received by server it will ignore action parameters because of URL encryption.

To solve this issue you can create global action filter by overriding OnActionExecuting method of controller class, in this you can extract the action parameters from the encrypted URL and these parameters can be set on filterContext to send plain text parameters to the actions.    



14. What is a HTML helper?


A HTML helper is a method that returns string; return string usually is the HTML tag. The standard HTML helpers (e.g. Html.BeginForm(),Html.TextBox()) available in MVC are lightweight as it does not rely on event model or view state as that of in ASP.Net server controls.

Tuesday, 14 May 2013

Linq tutorial for Beginners

What is LINQ?
LINQ stands for Language integrated Query, that allows us to query local object collection or remote data source.

Main Points:
1. Linq data has two parts, sequence and elements. Here names is a sequence and array members are elements.
2. Linq doesn't alter the input sequence. So result will always be in the order it was input.
3. Linq always return an IEnumerable result.
4. "n => n.EndsWith("a")" is a lambda expression that filters the result for us. 

Examples:
// Basic Query
// To achieve  Select * From Product  you need to write follofing linq Query
var Result1 = from p in db.Product
                 select p;

// To achieve  Select ProductID, ProductName, Price From Product you need to write follofing linq Query
    var Result = from p in db.Product
                 select new {
                     p.ProductID,
                     p.ProductName,
                     p.Price
                 };



// Where Clause Query
// To achieve  Select * From Product Where ProductID = 1 you need to write follofing linq Query  
  var Result = from p in db.Product
                 where p.ProductID == 1
                 select p;



// To achieve Select * From Product Where SupplierId =3 and Price > 10 you need to write follofing linq Query    
var Result = from p in db.Product
                 where p.SupplierID == 3 && p.Price > 10
                 select p;




// To achieve Select * From Product Where SupplierId =2 Or SupplierId=5 you need to write follofing linq Query

    var Result = from p in db.Product
                 where p.SupplierID == 2 || p.SupplierID == 5
                 select p;



// Order By Query
//To achieve   Select * From Product Order By ProductId you need to write follofing linq Query

    var Result = from p in db.Product
                 orderby p.ProductID
                 select p;



// To achieve  Select * From Product Order By ProductId Desc you need to write follofing linq Query

    var Result = from p in db.Product
                 orderby p.ProductID descending
                 select p;




// To achieve  Select * From Product Order By CategoryId, Price Desc you need to write follofing linq Query   
var Result = from p in db.Product
                 orderby p.CategoryID, p.Price descending
                 select p;



// Top Query
//To achieve Select Top 5 * From Product you need to write follofing linq Query

    var Result = (from p in db.Product
                 select p).Take(5);



//To achieve Select Top 1 * From Product you need to write follofing linq Query

    var Result = (from p in db.Product
                   select p).Take(1);
     or

    var Result = (from p in db.Product
                   select p).First();



// Distinct Query
//To achieve Select Distinct CategoryId From Product you need to write follofing linq Query

    var Result = (from p in db.Product
                   select p.CategoryID).Distinct();



// Group By Query
//To achieve Select CategoryId, Count(CategoryID) As FieldName From Product Group By CategoryId you need to write follofing linq Query

    var Result = from p in db.Product
                  group p by p.CategoryID into g
                  select new {
                      CategoryId = g.Key,
                      FieldName = g.Count()
                  };



//To achieve Select CategoryId, Avg(UnitPrice) As NewField From Product Group By CategoryId you need to write follofing linq Query

    var Result = from p in db.Product
                  group p by p.CategoryID into g
                  select new {
                      CategoryId = g.Key,
                      FieldName = g.Average(S => S.Price)
                  };



// Union Query
//To achieve Select * From Product Where CategoryId =1 union Select *  From Product Where CategoryId = 2 you need to write follofing linq Query

    var Result = (from p in db.Product
                   where p.CategoryID == 1
                   select p).Union(
                       from m in db.Product
                       where m.CategoryID == 2
                       select m
                   );