Books of .Net/Interview Questions Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Sunday, July 30, 2017

ASP.NET MVC Interview Questions and Answers 4


Question 1: What is Output Caching in MVC?
Answer: 
The main purpose of using Output Caching is to dramatically improve the performance of an ASP.NET MVC Application. It enables us to cache the content returned by any controller method so that the same content does not need to be generated each time the same controller method is invoked. Output Caching has huge advantages, such as it reduces server round trips, reduces database server round trips, reduces network traffic etc.
Keep the following in mind:
Avoid caching contents that are unique per user.
Avoid caching contents that are accessed rarely.
Use caching for contents that are accessed frequently.
Let's take an example. My MVC application displays a list of database records on the view page so by default each time the user invokes the controller method to see records, the application loops through the entire process and executes the database query. And this can actually decrease the application performance. So, we can advantage of the "Output Caching" that avoids executing database queries each time the user invokes the controller method. Here the view page is retrieved from the cache instead of invoking the controller method and doing redundant work.
Cached Content Locations
In the above paragraph I said, in Output Caching the view page is retrieved from the cache, so where is the content cached/stored?
Please note, there is no guarantee that content will be cached for the amount of time that we specify. When memory resources become low, the cache starts evicting content automatically.
OutputCache label has a "Location" attribute and it is fully controllable. Its default value is "Any", however there are the following locations available; as of now, we can use any one.

  1. Any
  2. Client
  3. Downstream
  4. Server
  5. None
  6. ServerAndClient

With "Any", the output cache is stored on the server where the request was processed. The recommended store cache is always on the server very carefully. You will learn about some security related tips in the following "Don't use Output Cache".

Question 2:What are the Folders in MVC application solutions?
Answer: Understanding the folders: 
When you create a project a folder structure gets created by default under the name of your project which can be seen in solution explorer. Below i will give you a brief explanation of what these folders are for. 

Model: This folder contains classes that is used to provide data. These classes can contain data that is retrived from the database or data inserted in the form by the user to update the database.

Controllers: These are the classes which will perform the action invoked by the user. These classes contains methods known as "Actions" which responds to the user action accordingly.

Views: These are simple pages which uses the model class data to populate the HTML controls and renders it to the client browser.


App_Start: Contains Classes such as FilterConfig, RoutesConfig, WebApiConfig. As of now we need to understand the RouteConfig class. This class contains the default format of the url that should be supplied in the browser to navigate to a specified page.



Question 3:What are the methods of handling an Error in MVC?

Answer: Exception handling may be required in any application, whether it is a web application or a Windows Forms application. 

ASP.Net MVC has an attribute called "HandleError" that provides built-in exception filters. The HandleError attribute in ASP.NET MVC can be applied over the action method as well as Controller or at the global level. The HandleError attribute is the default implementation of IExceptionFilter. When we create a MVC application, the HandleError attribute is added within the Global.asax.cs file and registered in the Application_Start event.

public static void RegisterGlobalFilters(GlobalFilterCollection filters)  
{  
    filters.Add(new HandleErrorAttribute());  
}  
protected void Application_Start()  
{  
    AreaRegistration.RegisterAllAreas();  
    RegisterGlobalFilters(GlobalFilters.Filters);  
    RegisterRoutes(RouteTable.Routes);  
}  
Important properties of HandleError attribute

The HandleError Error attribute has a couple for properties that are very useful in handling the exception.

ExceptionType: Type of exception to be catch. If this property is not specified then the HandleError filter handles all exceptions.

View: Name of the view page for displaying the exception information.

Master: Master View for displaying the exception.

Order: 

Order in which the action filters are executed. The Order property has an integer value and it specifies the priority from 1 to any positive integer value. 1 means highest priority and the greater the value of the integer is, the lower is the priority of the filter.

AllowMultiple: It indicates whether more than one instance of the error filter attribute can be specified.
Example
[HandleError(View = "Error")]  
public class HomeController: Controller  
{  
    public ActionResult Index()  
    {  
        ViewBag.Message = "Welcome to ASP.NET MVC!";  
        int u = Convert.ToInt32(""); // Error line  
        return View();  
    }  
}  

HandleError Attribute at Action Method Level,
Example
[HandleError(View = "Error")]  
public ActionResult Index()  
{  
    ViewBag.Message = "Welcome to ASP.NET MVC!";  
    int u = Convert.ToInt32(""); // Error line  
    return View();  

}  


Question 4:What is ViewStart?
Answer: Razor View Engine introduced a new layout named _ViewStart which is applied on all view automatically. Razor View Engine firstly executes the _ViewStart and then start rendering the other view and merges them. 

Example of Viewstart:
@ {  
    Layout = "~/Views/Shared/_v1.cshtml";  
} < !DOCTYPE html >  
    < html >  
    < head >  
    < meta name = "viewport"  
content = "width=device-width" / >  
    < title > ViewStart < /title> < /head> < body >  

    < /body> < /html>  

Question 5: What is GET and POST Actions Types?
Answer: 
GET is used to request data from a specified resource. With all the GET request we pass the URL which is compulsory, however it can take the following overloads.

.get(url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] ).done/.fail

POST
POST is used to submit data to be processed to a specified resource. With all the POST requests we pass the URL which is compulsory and the data, however it can take the following overloads. 


.post(url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )



You may also like



C# Programs frequently asked in Interview
1)Abstraction and Encapsulation in OOPS
2)Inheritance in OOPS
3)Polymorphism in OOPS
4)Interface in OOPS
5)What is Virtual Function
6)What is Abstract class and Abstract function
7)What is Static Class and Static Members
8)What is Collections
9) What is Generics
10)What is Delegate
11)Exception Handling
12)Static Constructor
13)MultiThreading in C#

1)How to make a Registration Page using CAPTCHA in C#

2)How to make a Login Window in C# - Step by Step

3)How to make PhoneBook in C#

4)Insert,Update ,Delete in a C#


2)C# Interview Questions and Answers Part 2

3)C# Interview Questions and Answers Part 3

4)C# Interview Questions and Answers Part 4    


1 comment :