ASP.NET MVC Interview Questions Part 6

31. What is Scaffolding?


Scaffolding is the technique for code generation for ASP.NET web application. This technique is used by many MVC frameworks like ASP.NET MVC, Node JS etc for code generation. Using Scaffolding we can reduce the amount of time to develop standard data operation in our project.

Scaffolding consists of page templates, entity page templates, field page templates, and filter templates. These are called Scaffolding templates.

32. What is JsonResult in ASP.NET MVC?


"JSON" (JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange. It provides an efficient mechanism to exchange data between the web browser and the web server.

The JSON format is an open standard format. The format of data looks very easy to understand and the data object consists of attribute-value pairs.

JsonResult Class is inherited from the "ActionResult" abstract class. Action methods in controllers return JsonResult that can be used in AJAX application. The JsonResult represents a class that is used to send JSON-formatted content to the response.

public ActionResult Movies()
    Return Json("Avengers!");

33. What is caching and when to use it?


Caching is used to enhance the performance of the ASP.NET MVC web application. Caching provides a way of storing frequently accessed data and reusing that data.

In ASP.NET MVC, OutputCache attribute is used for applying Caching. OutputCaching will store the output of a Controller in memory and if any other request comes for the same, it returns the output from cache result.

When to use:
  • Avoid caching content which is unique for every user or rarely used.
  • Cache frequently used content.
  • Define a short cache expiration time rather than disabling caching.

34. What is loose coupling and how is it possible?


Loose coupling is a software design where all classes can work independently without relying on each other.

MVC design pattern enables us to develop application's components independently which allows testing and maintenance of our application easier.

Using Dependency Injection, you can develop loosely coupled application's components.

35. How can we do exception handling in MVC?


In controller section we can override the "OnException" event and set the "Result" to the view name which we want to invoke when error occurs. In the code below you can see we have set the "Result" to a view named as "Error".

public class HomeController : Controller
    protected override void OnException(ExceptionContext filterContext)
        Exception ex = filterContext.Exception;
        filterContext.ExceptionHandled = true;
        var model = new HandleErrorInfo(filterContext.Exception, "Controller","Action");
        filterContext.Result = new ViewResult()
            ViewName = "Error",
            ViewData = new ViewDataDictionary(model)

To display above error in view, we use this code.


36. What is Layout View?


ASP.NET MVC layout is like Master page in ASP web form. Using Layout view we maintain a consistent look and feel across all the pages within our web application. Layout may contain common jQuery, CSS file across multiple Views.

The Layout view provides the code reusability that eliminates the duplicate code and enhances development speed and allows easy maintenance.
Following are the auto generated _Layout.cshtml.

<!DOCTYPE html>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("∼ /Content/css")
    @Scripts.Render("∼ /bundles/modernizr")
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
    <div class="container body-content">
        <hr />
            <p>© @DateTime.Now.Year - My ASP.NET Application</p>
    @Scripts.Render("∼ /bundles/jquery")
    @Scripts.Render("∼ /bundles/bootstrap")
    @RenderSection("scripts", required: false)

The _ViewStart File

The _ViewStart.cshtml in the Views folder contains the following content.
    Layout = "∼ /Views/Shared/_Layout.cshtml";