Saturday 25 February 2017

MVC interview questions Part 4

1. Ques: What is Caching ?  What are the advantages of Caching in MVC application and  How implement caching in MVC application ?
Answer:
Caching provides frequently accessed data from memory. It is technique to  improving web application’s performance.
Advantages of Caching 
  • Reduce hosting server round-trips:When content is cached at the client, it can eliminate request to server.
  • Reduce database server round-trips: When content is cached at the web server, it can eliminate the database request.
  • Reduce network traffic: When content is cached at the client side, it it also reduce the network traffic.
  • Avoid time-consumption for reusable content: When reusable content is cached, it avoid the time consumption for reusable content.
  • Improve performance :Since cached content reduce round-trips, network traffic and avoid time consumption for reusable content which cause a improved in the performance.
By using Output Cache Filter we can implement cache  technique in mvc application

OutputCache filter: The OutputCache filter allow you to cache the data that is output of an action method.
By default, this attribute filter cache the data till 60 seconds. After 60 sec, if a call was made to this action then ASP.NET MVC will cache the output again.

You can enable the output caching for an action method or controller by adding an [OutputCache] attribute as shown below:

[OutputCache(Duration=30, VaryByParam="none")]
public ActionResult Index()
{
 ViewBag.Message = DateTime.Now.ToString();
 return View();
}

The output of the Index() action method will be cached for 30 seconds. If you will not defined the duration, it will cached it for by default cache duration 60 sec.


2. Ques: What is Razor View Engine in MVC?
Answer:
  • The Razor View Engine is an advanced view engine, available with MVC 3.0 and later versions.
  • Namespace for ASPX view Engine is Web.Razor.
  • File Extension for this View Engine is .cshtml (Razor C#), for Views, Partial Views, Editor Template and Layout Pages. .vbhtml (Razor VB.NET), for Views, Partial Views, Editor Template and Layout Pages.
  • Supports TDD (Test Driven Development).
  • Razor Engine is little bit slow as compared to Web form Engine.
  • Razor Engine prevents XSS attacks(Cross-Site Scripting Attacks) means it encodes the script or html tags like <,> before rendering to view.
  • Razor syntax is easy to understand and much clean than Web Form syntax. Razor uses @ symbol to make the code.

3. Ques: Explain attribute based routing in MVC?
Answer:
  • Attribute based routing provides more control over the URIs by defining routes directly on actions and controllers in your ASP.NET MVC application and WEB API. 
  • It is new type of routing introduce in ASP.NET MVC5
attribute based routing Implement in two way one Controller level and another action level

Controller level attribute routing

[RoutePrefix("EmployeeHome")]
[Route("{action=index}")] //default action

public class EmployeeController : Controller
{
 //new route: /EmployeeHome/Index
 public ActionResult Index()
 {
 return View();
 }

 //new route: /EmployeeHome/About
 public ActionResult About()
 {
 ViewBag.Message = "Your application description page.";
 return View();
 }

 //new route: /EmployeeHome/Contact
 public ActionResult Contact()
 {
 ViewBag.Message = "Your contact page."; return View();
 }

}

Action level attribute routing

public class EmployeeController : Controller
{
 [Route("Employee/{id:int:min(100)}")] //route: /Employee/100
 public ActionResult Index(int id)
 {
 return View();
 }

 [Route("Employee/about")] //route" /Employee/about
 public ActionResult About()
 {
 ViewBag.Message = "Employee about page.";
 return View();
 }

 //route: /Home/Contact
 public ActionResult Contact()
 {
 ViewBag.Message = "Employee contact page.";
 return View();
 }
}


4. Ques:  What are Display Modes in MVC4 ?
Answer:
Display modes use a convention-based approach to allow selecting different views based on the browser making the request. The default view engine fi rst looks for views with names ending with .Mobile.cshtml when the browser’s user agent indicates a known mobile device. For example, if we have a generic view titled Index.cshtml and a mobile view titled Index.Mobile.cshtml, MVC 4 will automatically use the mobile view when viewed in a mobile browser.
Additionally, we can register your own custom device modes that will be based on your own custom criteria — all in just one code statement. For example, to register a WinPhone device mode that would serve views ending with .WinPhone.cshtml to Windows Phone devices, you’d use the following code in the Application_Start method of your Global.asax:

No comments:

Post a Comment

Thank you for comment