Monday, 17 March 2014

Interview Q & A on MVC in ASP.NET(Part 2)

?  Can we share a view across multiple controllers?
üYes, it is possible to share a view across multiple controllers by putting a view into the shared folder.

?   What is the use of a controller in an MVC application?
üA controller will decide what to do and what to display in the view. It works as follows:
-                      i) A request will be received by the controller
-                      ii) Basing on the request parameters, it will decide the requested activities
-                      iii) Basing on the request parameters, it will delegates the tasks to be performed
-                       iv) Then it will delegate the next view to be shown

?   What are the Core features of ASP.NET MVC?
üCore features of ASP.NET MVC framework are:
-                      Clear separation of application concerns (Presentation and Business Logic)
-                      An extensible and pluggable framework
-                      Extensive support for ASP.NET Routing
-                      Support for existing ASP.NET features

?   Explain about 'page lifecycle' of ASP.NET MVC ?
üThe page lifecycle of an ASP.NET MVC page is explained as follows:

i) App Initialisation:  In this stage, the aplication starts up by running Global.asax’s Application_Start() method.   In this method, you can add Route objects to the static RouteTable.Routes collection.
 If you’re implementing a custom IControllerFactory, you can set this as the active controller factory by assigning it to the System.Web.Mvc.ControllerFactory.Instance property.

ii) Routing:  Routing is a stand-alone component that matches incoming requests to IHttpHandlers by URL pattern.   MvcHandler is, itself, an IHttpHandler, which acts as a kind of proxy to other IHttpHandlers configured in the Routes table.

iii) Instantiate and Execute Controller:   At this stage, the active IControllerFactory supplies an IController instance.

iv) Locate and invoke controller action:  At this stage, the controller invokes its relevant action method, which after further processing, calls RenderView().

v) Instantiate and render view:  At this stage, the IViewFactory supplies an IView, which pushes response data to the IHttpResponse object.

?   Explain about the formation of Router Table in ASP.NET MVC ?
üIn the begining stage, when the ASP.NET application starts, the method known as Application_Start() method is called.
     The Application_Start() will then calls RegisterRoutes() method.
    This RegisterRoutes() method will create the Router table.

?   In an MVC application, what are the segments of the default route ?
üThere are 3 segments of the default route in an MVC application.They are:
1)Controller Name:  Example: Home
1)Action Method Name:  Example: Index
2)Parameter passed to the Action method:  Example: Parameter Id - MVC

?   Is the route {controller}{action}/{id} a valid route definition or not and why ?
ü{controller}{action}/{id} is not a valid route definition.   The reason is that, it has no literal value or delimiter between the placeholders. 
 If there is no literal, the routing cannot determine where to separate the value for the controller placeholder from the value for the action placeholder.
{controller}-{action}/{id}         //valid  ex.:  account-login/1
{controller}/{action}/{id}         //valid  ex.:  account/login/1

?   Is there any way to handle variable number of segments in a route definition?
üYou can handle variable number of segments in a route definition by using a route with a catch-all parameter.
 Example:        controller/{action}/{*parametervalues}
Here * reffers to catch-all parameter.

?   In an MVC application, which filter executes first and which is the last?
üAs there are different types of filters in an MVC application, the ‘Authorization filter’ is the one which executes first and ‘Exception filters’ executes in the end.

?   What are HTML Helpers?
üHTML helpers are methods we can invoke on the HTML property of a view. 

?   Difference between Viewbag and Viewdata in ASP.NET MVC ?
üView Data: In this, objects are accessible using strings as keys.
Example: 
Controller:
public ActionResult Index()
{
var companies = new List<string> { "NIT",  "Nacre" };
ViewData["companies"] = companies;
return View();
}

View:
<ul>
@foreach (var company in (List<string>)ViewData["companies"])
{ <li>  @company </li>
}
</ul>

View Bag: It will allow the object to dynamically have the properties add to it.

Example:
Controller:
public ActionResult Index()
{
var companies = new List<string> { "NIT",  "Nacre" };
ViewBag.companies = companies;
return View();
}

View:
<ul>
@foreach (var company in ViewBag.companies)
{
<li>  @company </li>
}
</ul>

An important point to note is that there is no need to cast our object when using the ViewBag. This is because the dynamic we used lets us know the type.

?   What are Validation Annotations?
üData annotations are attributes you can find in System.ComponentModel.DataAnnotations namespace. These attributes provide server-side validation, and the framework also supports client-side validation when you use one of the attributes on a model property. You can use four attributes in the DataAnnotations namespace to cover common validation scenarios:
-                      Required
-                      StringLength
-                      Compare
-                      RegularExpression
-                      Range

?   What are the file extensions for razor views?
üThere are two types of file extensions for razor views.   They are:
i) .cshtml (C#)   ii) .vbhtml (VB.Net).

?   What is partial view in Asp.Net Mvc?
üA partial view is like as user control in Asp.Net web form. Partial views are reusable views like as Header and Footer. It has .csHtml extension.   Partial() and RenderPartial() Html method renders partial view in Mvc razor.

<div>@Html.Partial("_Comments")</div>
<div>Full path:  @Html.RenderPartial("~/Views/Shared/_Product.cshtml");</div>

?   What is strongly typed view in MVC?
üStrongly type view is used for rendering specific type of model object. By specifying the type of data we get intellisense for that model class.

Exmaple:  return View(employee);

?   How to call an action Method on the click of a link?
ü@Html.ActionLink("LinkName", "ActionMethod", "Controller")

Example:  @Html.ActionLink("Edit", "Edit", "Employee")

?   What is the use of ViewStart in ASP.NET MVC?
üIn the default template of ASP.NET MVC, we get _ViewStart.cshtml page that is used to almost similar to MasterPage in ASP.NET Web Form or like a layout template.

?   In case we are returning anonymous type from Controller to the views, what should be the @model in the View?

ü@model IEnumerable<dynamic variable>

?   What are different ways of returning a View?
üThere are different ways for returning/rendering a view in MVC Razor. E.g. return View(), return RedirectToAction(), return Redirect() and return RedirectToRoute().

**       ***           **



No comments:

Post a Comment