ASP.NET MVC Interview Questions Part - IIl Day 27

Lecture 27 : ASP.NET MVC Interview Questions Part - III

1) What is dependency injection ?
  • Injecting the dependent object by some one else rather than controller creating.

  • Dependency injection is a design pattern in which a class requests dependencies from external sources rather than creating them.

  • Angular's DI framework provides dependencies to a class upon instantiation. 

  • You can use Angular DI to increase flexibility and modularity in your applications.

2) How can we read Configuration data from appsettings.json ?

Getting Values from appsettings.json :
  • There are one or more ways available to fetch values from appsettings.json file in .NET Core.

  • Following are the ways we are going to learn :

Using IConfiguration :
  • The IConfiguration is available in the dependency injection (DI) container, so you can directly access JSON properties by simply injecting IConfiguration in the constructor of a controller or class. 

  • It represents a set of key/value application configuration properties.

IConfiguration is used as follows :
  • Create a simple web or web API project in .NET Core.

  • Create a controller or use default one.

How to read values from Appsettings.json file :
  • There are multiple ways to read value from appsetting.json file First we learn simple way like how we used to read value from web.config appsetting section.

  • In asp.net core we need to refer a namespace  Microsoft.Extensions.Configuration;

  • Then use IConfiguration to read the value from appsettings.json file. 

  • In Controller constructor we need to set the local IConfiguration property this way :

See this code illustration :

3) What is the need of viewData ?
  • ViewData is help to pass data from controller to the view.

  • Where data is passed in the form of key-value pair. And typecasting is required to read the data in View if the data is complex and we need to ensure null check to avoid null exceptions.

4) What is the difference between viewData and viewBag ?

ViewData :
  • ViewData is property of ControllerBase class.

  • ViewData is a type of dictionary object.

  • ViewData is key-value dictionary collection.

  • ViewData was introduced in MVC 1.0 version.

  • ViewData works with .Net framework 3.5 and above.Need to do type conversion of code while enumerating.

  • ViewData object keeps data only for current request.

  • Requires typecasting for complex data type and checks for null values to avoid error.

  • Example :

In Controller :

public ActionResult Index()

{

ViewData["students"] = "studentList";

returnView();

}

In View :

@ViewData["students"];


ViewBag : 
  • ViewBag is property of ControllerBase class.

  • ViewBag is a type of dynamic object.

  • ViewBag is a type of object.

  • ViewBag was introduced in MVC 3.0 version.

  • ViewBag works with .Net framework 4.0 and above.

  • ViewBag uses property and handles it, so no need to do type conversion while enumerating.

  • ViewBag object keeps data only for current request.

  • ViewBag can check for nulls syntactical cleaner.

  • Doesn’t require typecasting for complex data type.

  • Example :

In Controller :

public ActionResult Index()

{

ViewBag["Id"] = "1";

returnView();

}

In View :

@ViewBag.Id;


ViewData Vs ViewBag :

Program :


Comments

Popular posts from this blog

Basic Concept In C Sharp Day 16

VS Code In JavaScript Day 6

ASP.NET MVC Interview Questions Part - II Day 26