In this article, I am going to discuss the Most Frequently asked ASP.NET MVC Caching Interview Questions and Answers. Please read our previous article where we discussed the most frequently asked Filters Interview Questions in ASP.NET MVC Application with Answers.
What is Caching?
Caching is the 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 the web application’s performance.
When to use Caching in ASP.NET MVC Application?
We are getting the following advantages of using caching:
The OutputCache filter allows you to cache the data that is the output of an action method. By default, this attribute filter caches the data until 60 seconds. After 60 sec, ASP.NET MVC will execute the action method again and cache the output again.
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 webserver, 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.
How to allow users to submit HTML tags in ASP.NET MVC?
By default, ASP.NET MVC doesn’t allow a user to submit HTML for avoiding the Cross-Site Scripting attack on your application. You can achieve it by using the ValidateInput attribute and AllowHtml attribute. The ValidateInput attribute can enable or disable input validation at the controller level or at any action method.
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 the AllowHtml attribute.
What is loose coupling and how is it possible in MVC Design Pattern?
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 that 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:
What is Caching?
Caching is the 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 the web application’s performance.
When to use Caching in ASP.NET MVC Application?
- Use caching for contents that are accessed frequently.
- Avoid caching for contents that are unique per user.
- Avoid caching for contents that are accessed infrequently/rarely.
- 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.
- For efficient caching use a 64-bit version of Windows Server and SQL Server.
- For database caching make sure your database server has sufficient RAM otherwise, it may degrade the performance.
- For caching of dynamic contents that change frequently, define a short cache–expiration time rather than disabling caching.
We are getting the following advantages of using caching:
- Reduce hosting server round-trips
- When content is cached at the client or in proxies, it causes the minimum request to the 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 also reduces the network traffic.
- Avoid time-consumption for regenerating reusable content
- When reusable content is cached, it avoids the time consumption for regenerating reusable content.
- Improve performance
- Since cached content reduces round-trips, network traffic and avoids time consumption for regenerating reusable content which causes a boost in the performance.
The OutputCache filter allows you to cache the data that is the output of an action method. By default, this attribute filter caches the data until 60 seconds. After 60 sec, ASP.NET MVC will execute the action method again and cache the output again.
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 webserver, 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.
How to allow users to submit HTML tags in ASP.NET MVC?
By default, ASP.NET MVC doesn’t allow a user to submit HTML for avoiding the Cross-Site Scripting attack on your application. You can achieve it by using the ValidateInput attribute and AllowHtml attribute. The ValidateInput attribute can enable or disable input validation at the controller level or at any action method.
[ValidateInput(false)] 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 the AllowHtml attribute.
public class BlogModel { [Required] [Display(Name = "Title")] public string Title { get; set; } [AllowHtml] [Required] [Display(Name = "Description")] public string Description { get; set; } }
What is loose coupling and how is it possible in MVC Design Pattern?
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 that 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:
- NUnit – This is the most popular unit testing framework 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.
- xUnit.NET – This provides a way to run automated unit tests. It is simple, easily extended, and has a very clean syntax.
- Ninject – This provides a way to wire up classes in your application.
- Moq – This provides a framework for mocking interfaces and classes during testing.
0 comments:
Post a Comment
If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.