NET Centric Computing · Unit 4 · 10 hrs
Creating ASP.NET core MVC applications
Exam-focused notes for Creating ASP.NET core MVC applications (NET Centric Computing, CSC378): what the TU syllabus asks and how it has actually been tested, with 9 solved past questions from this unit.
Creating ASP.NET core MVC applications in short
This unit builds an actual ASP.NET Core MVC application: the project's environment and startup, controllers and the action results they can return, rendering views with Razor, tag helpers, model binding and validation, URL routing, Web API controllers, and wiring dependencies together with the built-in dependency injection container.
What this unit covers
- Setting up the Environment
- Controllers and Actions: Create Controllers, Create Actions and Action Results Types
- Rendering HTML with Views: Razor Syntax
- Understanding Tag Helpers
- Models: Binding and Validations
- URL Routing and features
- Web API Applications: API Controllers, JSON
- Dependency Injection and IOC containers
Controllers and Actions
2081 · 10 marks
How do you create controller? Illustrate with an example. Describe the procedure of rendering HTML with Razor with scenario.[10]
2080 · 5 marks
How do you create controller? Mention some requirements for rendering HTML. [5]
Rendering HTML with Views
2081 · 5 marks
How does ASP.NET core MVC provides patterns-based way to build dynamic website? Explain. [5]
Dependency Injection and IOC containers
2081 · 5 marks
Describe the life cycle of Dependency Injection container. [5]
Show solved answerHide answer
Life Cycle of Dependency Injection (DI) Container
In ASP.NET Core, the built-in IoC (Inversion of Control) container manages the creation and lifetime of service instances. The lifetime of a service defines how long an instance lives and when a new instance is created. There are three main service lifetimes:
1. Transient
- The IoC container creates a new instance every time the service is requested.
- Each call to resolve the service produces a fresh object.
- Best suited for lightweight, stateless services.
Registration Example:
services.Add(new ServiceDescriptor(
typeof(ILog),
typeof(MyConsoleLogger),
ServiceLifetime.Transient));
Use case: A simple utility/helper service that holds no shared state.
2. Scoped
- The IoC container creates one instance per HTTP request and shares that same instance throughout the entire request.
- Different requests get different instances.
- Best suited for services that need to maintain state within a single request (e.g., database context).
Registration Example:
services.Add(new ServiceDescriptor(
typeof(ILog),
typeof(MyConsoleLogger),
ServiceLifetime.Scoped));
Use case: A database context or unit-of-work that should be consistent within one request.
3. Singleton
- The IoC container creates only one instance for the entire application lifetime.
- The same instance is reused for every request and every user.
- Best suited for services that are stateless and expensive to create, or that hold shared application-wide state.
Registration Example:
services.Add(new ServiceDescriptor(
typeof(ILog),
new MyConsoleLogger())); // single instance passed directly
Use case: A logging service or configuration service shared across the whole application.
Summary Table
| Lifetime | Instance Created | Shared Within |
|---|---|---|
| Transient | Every time requested | Not shared |
| Scoped | Once per HTTP request | Single request |
| Singleton | Once per application | Entire application |
How Registration Works
Services are registered inside the ConfigureServices() method in Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// Singleton
services.Add(new ServiceDescriptor(typeof(ILog), new MyConsoleLogger()));
// Transient
services.Add(new ServiceDescriptor(typeof(ILog),
typeof(MyConsoleLogger), ServiceLifetime.Transient));
// Scoped
services.Add(new ServiceDescriptor(typeof(ILog),
typeof(MyConsoleLogger), ServiceLifetime.Scoped));
}
Once registered, the IoC container automatically injects the service via constructor injection wherever the service type is used as a constructor parameter, without the developer needing to manually instantiate it.
2080 · 5 marks
Mention the two types of services managed by IoC container and how they are registered? [5]
Show solved answerHide answer
Two Types of Services Managed by IoC Container and Their Registration
Types of Services (Lifetimes)
The built-in IoC container in ASP.NET Core supports three kinds of service lifetimes, of which the two most commonly highlighted types are:
1. Singleton
- The IoC container creates and shares a single instance of a service throughout the application's lifetime.
- The instance is created the first time it is requested, and every subsequent request uses that same instance.
- Suitable for services that maintain global state or are expensive to create.
2. Transient
- The IoC container creates a new instance of the specified service type every time it is requested.
- This lifetime works best for lightweight, stateless services.
- Each request gets a completely fresh object.
3. Scoped (also managed by IoC)
- The IoC container creates one instance per HTTP request.
- The same instance is shared within a single request but a different instance is created for different requests.
How Services Are Registered
Services are registered inside the ConfigureServices method using the IServiceCollection interface. Registration is done using ServiceDescriptor:
public void ConfigureServices(IServiceCollection services)
{
// Singleton registration
services.Add(new ServiceDescriptor(typeof(ILog), new MyConsoleLogger()));
// Transient registration
services.Add(new ServiceDescriptor(
typeof(ILog),
typeof(MyConsoleLogger),
ServiceLifetime.Transient));
// Scoped registration
services.Add(new ServiceDescriptor(
typeof(ILog),
typeof(MyConsoleLogger),
ServiceLifetime.Scoped));
}
Summary Table
| Lifetime | Instance Created | Shared? |
|---|---|---|
| Singleton | Once (first request) | Across entire application |
| Transient | Every request | Not shared |
| Scoped | Once per HTTP request | Within the same request only |
Once registered, the IoC container automatically performs constructor injection wherever the service type is used as a constructor parameter.
2079 · 5 marks
What does dependency injection and IOC container means? Explain. [5]
Understanding Tag Helpers
2079 · 5 marks
What is tag helper? Discuss about razor syntax with an example. [5]
Setting up the Environment
Make Unit 4 stick
Practice CSC378 with flashcards & quizzes