Q & A - Asp.Net Questions & Answers


Q&A 1.

How to use caching in Asp.Net MVC?

Add OutputCache attribute to Action Index() method in controller like below.

[OutputCache(Duration = 120, VaryByParam = "none"]  
public ActionResult Index()  
    return view();

This will make mvc method Index() being cached for 120 secs and return the same view before it expires.

Q&A 2.

Explain AsNoTracking() method in EF?

AsNoTracking() method helps to optimize and tune performance for the Linq to SQL queries as Entity Framework will not track the results of the query. This means that Entity Framework performs no additional processing or storage of the entities which are returned by the query. However, it also means that we can not update the entities without reattaching them to the tracking graph. This helps improve performance over regular LINQ queries.

Q&A 3.

When we should use AsNoTracking() method?

  1. First is when we retrieve any data entity object in linqtosql queries & we need not to update data in db. As it is not getting tracked by EF Context.
  2. Secondly, AsNoTracking() allows the "unique key per record" requirement in EF to be bypassed. Meaning when using  AsNoTracking() generating view will allow duplicate keys in records.

Q&A 4.

How to specify routes in Asp.Net RouteConfig.cs?

Add any new routes before default route is defined in RouteConfig.cs as routes are added in order to the route table. While routes are evaluated for a match to an incoming URL, Asp.Net MVC will search for any new routes before default route as it is pretty general and may inadvertently match a URL intended for a different handler.

Q&A 5.

What is attribute routing in Asp.Net MVC?

Attribute Routing allows us to define routes over actions and controllers in ASP.NET MVC5 and WEB API 2.

public class HomeController : Controller
 [Route("employee/{id:int:min(100)}")] //route: /employee/100
 public ActionResult Index(int id)
 //TO DO:
 return View();

Q&A 6.

What are POCO model classes in Entity Framework?

A POCO model class is Plain Old CLR Objects. These POCO entities (also known as persistence-ignorant objects) support most of the same LINQ queries as Data Entity Model Classes.
POCO model classes are not dependent on any framework-specific base class and it is like any other normal .NET class, due to this they are called Plain Old CLR Objects.

Q&A 7.

Three different approaches while using Entity Framework in connecting database?

There are the following approaches, which are used to connect the database with the application.

  1. Database First
  2. Model First
  3. Code First

Database First in Entity Framework

In the database first approach, you generate the context and entities for the existing database using EDM wizard integrated in Visual Studio or executing EF commands.

Code First Approach in Entity Framework

The Code First approach helps you to create the entities in your application by focusing on the domain requirements. Use this approach when you do not have an existing database for your application. Create model classes for entities and add context class then generate the database from these classes using migration commands.

Model First Approach in Entity Framework

In the model-first approach, you create entities, keys, relationships, and inheritance hierarchies directly on the visual designer integrated in Visual Studio and then generate entities, the context class, and the database script from your visual model.

Q&A 8.

What is the use of ViewModel in Asp.Net MVC?

ViewModel is a plain class with properties, which is used to bind it to strongly typed view. ViewModel can have the validation rules defined for its properties using data annotations.

Q&A 9.

What are Validation Annotations?

Data annotations are attributes which can be found in the "System.ComponentModel.DataAnnotations" namespace. These attributes will be used for server-side validation and client-side validation is also supported.
Such as - Required field, String Length, Regular Expression and Range field validators.

Q&A 10.

Explain the concept of MVC Scaffolding in Asp.Net?

ASP.NET Scaffolding is a code generation framework for ASP.NET MVC Web applications. Visual Studio 2013 includes pre-installed code templates for MVC and Web API projects. With scaffolding we can quickly add CRUD (Create,Read,Update and delete) operations for database and view for all functions and controller with each action method created. ASP.NET Scaffolding helps to reduce our time for developing standard data operations for our web application and allows us to quickly build a functional data-driven Website.

Related Quiz Content

This website is using cookies to make user experience of the site better. More info. OK