MVC Caching Interview Questions

ASP.NET MVC Caching Interview Questions and Answers

In this article, I will discuss Most Frequently asked MVC Caching Interview Questions and Answers.

How to allow HTML tags in ASP.NET MVC?

By default, ASP.NET MVC doesn’t allow a user to submit HTML for avoiding Cross Site Scripting attack to your application. You can achieve it by using ValidateInput attribute and AllowHtml attribute.

The ValidateInput attribute can enable or disable input validation at the controller level or at any action method.

public class HomeController : Controller
    public ActionResult AddArticle()
        return View();

The ValidateInput attribute allows the Html input for all the properties and that is unsafe. Since you have enabled Html input for only one-two properties then how to do this. To allow Html input for a single property, you should use AllowHtml attribute.

public class BlogModel
    [Display(Name = "Title")]
    public string Title { get; set; }

    [Display(Name = "Description")]
    public string Description { get; set; }
What is caching and when to use it?

The Caching is a most important aspect of the high-performance web application. Caching provides a way of storing frequently accessed data and reusing that data. Practically, this is an effective way of improving web application’s performance.

When to use caching
  1. Use caching for contents that are accessed frequently.
  2. Avoid caching for contents that are unique per user.
  3. Avoid caching for contents that are accessed infrequently/rarely.
  4. Use the VaryByCustom function to cache multiple versions of a page based on customization aspects of the request such as cookies, role, theme, browser, and so on.
  5. For efficient caching use 64-bit version of Windows Server and SQL Server.
  6. For database caching make sure your database server has sufficient RAM otherwise, it may degrade the performance.
  7. For caching of dynamic contents that change frequently, define a short cache–expiration time rather than disabling caching.
What are the advantages of caching?    

There are following advantages of caching:

  1. Reduce hosting server round-trips           
  2. When content is cached at the client or in proxies, it causes the minimum request to the server.         
  3. Reduce database server round-trips        
  4. When content is cached at the web server, it can eliminate the database request.     
  5. Reduce network traffic 
  6. When content is cached at the client side, it also reduces the network traffic.              
  7. Avoid time-consumption for regenerating reusable content            
  8. When reusable content is cached, it avoids the time consumption for regenerating reusable content.   
  9. Improve performance  
  10. Since cached content reduces round-trips, network traffic and avoid time consumption for regenerating reusable content which causes a boost in the performance.   
What is output caching?

The OutputCache filter allows 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, ASP.NET MVC will execute the action method again and cache the output again.

public class HomeController : Controller
    [OutputCache(Duration = 20, VaryByParam = "none")]
    public ActionResult Index()
        ViewBag.Message = DateTime.Now.ToString(); return View();

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

Output Caching Location

By default, content is cached in three locations: the web server, any proxy servers, and the user’s browser. You can control the content’s cached location by changing the location parameter of the OutputCache attribute to any of the following values: Any, Client, Downstream, Server, None, or ServerAndClient.

By default, the location parameter has the value and which is appropriate for most of the scenarios. But sometimes there are scenarios when you required more control over the cached data.

What is loose coupling and how is it possible?

One of the most important features of the MVC design pattern is that it enables separation of concerns. Hence you can make your application’s components independent as much as possible. This is known as loose coupling and it makes testing and maintenance of our application easier. Using Dependency Injection you can make your application’s components more loosely coupled.

What is Test Driven Development (TDD)?

TDD is a methodology which says; write your tests first before you write your code. In TDD, tests drive your application design and development cycles. You do not do the check-in of your code into source control until your entire unit tests pass.

What are the commonly used tool for Unit Testing in ASP.NET MVC?

ASP.NET MVC has been designed for testability without dependencies on the IIS server, on a database, or on external classes. There are following popular tools for ASP.NET MVC testing:

  1. NUnit – This is the most popular unit testing frameworks for Microsoft .NET. Its syntax is relatively simple and easy to use. It comes with a test runner GUI and a command-line utility. NUnit is also available as a NuGet package for download.
  2. xUnit.NET – This provides a way to run automated unit tests. It is simple, easily extended, and has a very clean syntax.
  3. Ninject 2 – This provides a way to wire up classes in your application.
  4. Moq – This provides a framework for mocking interfaces and classes during testing.

In this article, I try to explain most frequently asked MVC Caching Interview Questions and Answers. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *