InterviewSolution
| 1. |
What is Output Caching in MVC |
|
Answer» Like in ASP.Net web forms, we have a toolbox for adding controls (like textbox, label, dropdown) on any particular page. Now, in the ASP.NET MVC application, there is no toolbox available to add controls on the view. ASP.NET MVC PROVIDES HtmlHelper class which contains various methods that help you create HTML controls programmatically. All HtmlHelper methods generate HTML and return the result as a string. The final HTML is generated at runtime by these functions. The HtmlHelper class is designed to generate UI and it should not be used in controllers or models. The following is the list of Html Helper controls. Html.Beginform Html.EndForm Html.Label Html.TextBox Html.TextArea Html.Password Html.DropDownList Html.CheckBox Html.RedioButton Html.ListBox Html.Hidden Below are Strongly Type Html Helper methods, this will allow us to check compile-time errors. We get Model's Property intelligence at Runtime. Html.LabelFor Html.TextBoxFor Html.TextAreaFor Html.DropDownListFor Html.CheckBoxFor Html.RadioButtonFor Html.ListBoxFo Output Caching is part of state management. Output Caching is one of the types of Action Filter. Output caching can help to improve the performance of MVC application by caching the content RETURNED by any controller action. It reduces the server and database round trips and also reduces network traffic. Enabling Output Caching Output Caching can be enabled by adding attribute: [OutputCache] to either on controller or action method. If added to the top of the controller then output caching is enabled for all the action methods of that controller. Example: public class HomeController : Controller { [OutputCache(Duration=10, VaryByParam="none")] public ActionResult Index() { return View(); } }Where the content is Cached By default, the content is cached in three locations: web server, proxy server, and web browser. But we can control where exactly we want to cache the content by modifying the location property of [OutputCache]. Example: [OutputCache(Duration=3600, VaryByParam="none", Location=OutputCacheLocation.Client)] public ActionResult Index() { return View(); }Any of the value for Location property can be selected as FOLLOWS:
Creating a Cache PROFILE MVC has provided one more feature to create a caching profile. Creating a caching profile on a configuration file (web.config) provides a couple of advantages like:
Example: <caching> <outputCacheSettings> <outputCacheProfiles> <add name="Cache1HourTest" duration="3600" varyByParam="none" Location=OutputCacheLocation.Any/> </outputCacheProfiles> </outputCacheSettings> </caching>Html.HiddenFor HtmlHelper class generates html elements. For example, @Html.ActionLink("Create New", "Create") would generate anchor tag <a href="/Student/Create">Create New</a>. |
|