ASP.NET MVC Interview Questions Part - IIl Day 27
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.
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 :
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.
Create a simple web or web API project in .NET Core.
Create a controller or use default one.
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 :
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.
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 :
public ActionResult Index()
{
ViewData["students"] = "studentList";
returnView();
}
In View :
@ViewData["students"];
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.
Comments
Post a Comment