0% found this document useful (0 votes)
3 views84 pages

Unit-2 Net Core Framework

The document outlines the setup and configuration of the ASP.NET Core MVC framework, detailing the process of creating a new ASP.NET Core application and adding MVC services and middleware. It covers routing mechanisms, including conventional and attribute-based routing, and explains how to define and manage routes within an application. Additionally, it describes the roles of various middleware components in handling incoming HTTP requests and directing them to the appropriate controllers and actions.

Uploaded by

suchitab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views84 pages

Unit-2 Net Core Framework

The document outlines the setup and configuration of the ASP.NET Core MVC framework, detailing the process of creating a new ASP.NET Core application and adding MVC services and middleware. It covers routing mechanisms, including conventional and attribute-based routing, and explains how to define and manage routes within an application. Additionally, it describes the roles of various middleware components in handling incoming HTTP requests and directing them to the appropriate controllers and actions.

Uploaded by

suchitab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

CVM UNIVERSITY

INSTITUTE OF SCIENCE & TECHNOLOGY FOR ADVANCED STUDIES & RESEARCH


(ISTAR)
M.Sc. (Information Technology)
Semester – II

Paper Code: 101410212


Paper Title: .NET CORE FRAMEWORK

Prepared By:
Dr. Suchita Patel
Assistant Professor
M.Sc. (IT) Department, ISTAR

Unit 2: Controllers & Action Methods: Controllers Overview; Action


Methods and IActionResult object; Passing data from Controller to View;
Understanding Action Selectors; Action Filters; Building Custom Action
Filters; Middleware; Asynchronous Action Methods

ASP.NET Core MVC – Basic


ASP.NET Core MVC – Data Passing Techniques
ASP.NET Core MVC – Routing
Filters in ASP.NET Core MVC
ASP.NET Core MVC – Action Results
How to Setup MVC in ASP.NET Core Application:
In ASP.NET Core (.NET), it is possible to build an entire application using only the
ASP.NET Core Middleware component. However, the ASP.NET Core MVC framework
provides the features we can use to create HTML Pages and HTTP-based APIs easily. So,
I will show you how to set up MVC in the ASP.NET Core Application.

Creating a new ASP.NET Core Empty Application:


Let us first create an empty ASP.NET Core application. Later, in this ASP.NET Core MVC
article series, we will see examples using the ASP.NET Core Web Application (Model-
View-Controller) Project template. To create a new Empty ASP.NET Core Web
Application, open Visual Studio 2022 and click the Create a new project tab, as shown in
the image below.

Once you click on the Create a new project tab, the Create a new project window will
open. In this window, select the ASP.NET Core Empty project template and click the
Next button, as shown in the image below.

Once you click on the Next button, it will open the Configure Your New Project window.
Here, provide the necessary information to create a new project. First, give an
appropriate name for your project (FirstCoreMVCWebApplication), set the location
where you want to create this project, and the solution name for the ASP.NET Core Web
application. And finally, click on the Create button, as shown in the image below.

Once you click on the Next button, it will open the Additional Information window.
Here, you need to select .NET Framework. You also need to check the Configure for
HTTPS and do not use top-level statements check boxes. Finally, click the Create
button, as shown in the image below.

Once you click the Create button, a new ASP.NET Core Web Application will be created
in Visual Studio 2022 using .NET 8. The project will have the following file and folder
structure.

By default, the Empty template does not include the MVC setup. Let’s see how to set up
MVC in the ASP.NET Core application.
How to Setup MVC in ASP.NET Core Application:

Setting up the MVC (Model-View-Controller) in the ASP.NET Core Application involves


two steps: Configuring the Required MVC Services and Middleware Components in the
Request Processing Pipeline.

Adding MVC Service to the Request Processing Pipeline:


First, we need to add the required MVC services to the Application Request Processing
Pipeline. To do so, we need to modify the Main method of the Program class as follows.
The builder.Service.AddMVC() statement will include all the services required to
develop the ASP.NET Core MVC application. Once you add this, you can use Models,
Controllers, Views, TempData, ViewData, ViewBag, and many other features in your
ASP.NET Core MVC Application.

Note: In ASP.NET Core, along with the AddMVC() method, we also have the
AddControllersWithViews() method.

Adding Controller in ASP.NET Core MVC Application:


In the ASP.NET Core MVC application, all the Controllers should be in a specific folder
named Controllers. So first, create a folder named Controllers within the project root
directory. Once you add the Controllers folder, add a new class file
named HomeController.cs within the Controllers folder. Once you add the
HomeController class, your project folder structure should look as shown below.

Now open the HomeController.cs class file and copy and paste the following code. To
make a class as a controller in ASP.NET Core MVC, that class must be inherited from
the Controller base class. So, you can see in the below code that our controller, i.e.,
HomeController, is inherited from the Controller base class. This Controller base class
belongs to Microsoft.AspNetCore.Mvc namespace.
With the above changes in place, now run the application, and you will get the following
output.

We are not getting the output from the HomeController’s Index action method. The
above output comes from the MapGet method, which you can find within the Program
class’s Main method.

How Do We Configure our Index Action Method of HomeController as the Default


Route?

We need to tell the ASP.NET Core MVC Framework to use the Index action method of
our Home Controller as the default route. To do so, we must add the Required MVC
Middleware Component (UseRouting and MapDefaultControllerRoute) to the
application request processing pipeline.

UseRouting Middleware in ASP.NET Core:

The UseRouting middleware adds the routing capabilities to the request processing
pipeline. Its primary objective is to match incoming HTTP requests to the corresponding
route endpoint definitions.

By adding UseRouting, we enable the application to use route matching.

It matches the incoming request URL to the routes that are defined in the application.
This is essential for directing requests to the appropriate controllers and actions.

It should be placed before middleware, which depends on route matching, such as


MapDefaultControllerRoute.

Syntax: app.UseRouting();
MapDefaultControllerRoute Middleware in ASP.NET Core:
The MapDefaultControllerRoute method is a shorthand method to configure the default
route for MVC controllers. Its primary objective is to set up a conventional routing
pattern that maps URLs to controllers and actions using a predefined template.

 It uses the default routing template {controller=Home}/{action=Index}/{id?}.


This means that if no specific route is provided, the request will be directed to
the HomeController and its Index action.
 This method ensures that requests are routed to the appropriate controller and
action method based on the URL.
 Syntax: app.MapDefaultControllerRoute();

So, modify the Main Method of the Program class as shown below. The UseRouting()
and MapDefaultControllerRoute() Middleware Components add the MVC
Middleware to the Request Processing Pipeline.

Run the application, and you should get the expected output, as shown in the image
below.
How do we specify a custom controller and action method?

The MapDefaultControllerRoute() middleware uses Home as the default Controller and


Index as the default action method for our application. This is why when we run the
application, the Index action method of the Home Controller handles the request. But
you can also change this default behavior. To do so, we need to use the
MapControllerRoute Middleware component instead of the
MapDefaultControllerRoute() middleware and specify the default controller and action.

MapControllerRoute Middleware Component:

The MapControllerRoute method is used to define a custom route for MVC controllers in
an ASP.NET Core application. Its primary objective is to map incoming HTTP requests to
specific controllers and actions based on a defined URL pattern.

It allows us to specify custom URL patterns that map to controllers and actions,
providing flexibility in how URLs are structured.

It also defines a custom URL pattern that specifies how URLs should be parsed and
matched to controller actions.

We can also define multiple routes using MapControllerRoute to handle different URL
patterns and route them to different controllers and actions.

So, modify the Main method of the Program class to use the MapControllerRoute
Middleware component instead of the MapDefaultControllerRoute() middleware as
follows.
Now, run the application, and you should get the output as expected.

So, in short, to set up MVC in the ASP.NET Core Web Application, we first need to add
the required MVC Services to the dependency injection container and then configure the
required MVC Middleware Components to the Request Processing Pipeline.

ASP.NET Core MVC – Routing:


 What is Routing in ASP.NET Core MVC?
 How Does Routing Work in ASP.NET Core?
 What are the Different Types of Routing Supported by ASP.NET Core
MVC?
 What is Conventional Based Routing in ASP.NET Core MVC Web
Application?
 What is Attribute-Based Routing in ASP.NET Core MVC Application?
 Understanding Conventional-Based Routing in ASP.NET Core MVC.
 Understanding the Default Route in ASP.NET Core MVC Web Application.
 Understanding Route Template in ASP.NET Core MVC Application.
 Configure Multiple Conventional Routing in ASP.NET Core MVC
Application.
What is Routing in ASP.NET Core MVC?
Routing in ASP.NET Core MVC is a mechanism that inspects the incoming
HTTP request (i.e., URLs) and then maps those HTTP requests to the
appropriate controller actions. It enables the framework to determine what
code to execute based on the URL of the request. Routing is an important
component of ASP.NET Core MVC applications, providing the ability to
define URL patterns and manage how URLs are processed and handled by
the application.

How Does Routing Work in ASP.NET Core?


In simple words, Routing is a pattern-matching system that monitors an
incoming HTTP request and decides what to do with that incoming HTTP
request. When the client makes a request, i.e., an HTTP Request, the routing
engine first receives it. Once the routing engine receives an HTTP request,
the routing engine tries to find the route pattern that matches the
requested URL with already registered routes. Routes contain information
about the Controller name, action method name, method type (Get, Post,
Put, Patch, Delete), method parameters, route data, etc.

If it finds a matching URL pattern for the incoming request, it forwards the
request to the appropriate controller and action method. If there is no
match for the incoming HTTP request URL Pattern, it returns a 404 HTTP
status code to the client. For a better understanding, please have a look at
the following diagram.
What are the Different Types of Routing Supported by ASP.NET Core
MVC?

We can configure routes in the ASP.NET Core MVC Web Application in two
ways. They are as follows:

1. Convention-Based Routing
2. Attribute-Based Routing.

What is Conventional Based Routing in ASP.NET Core MVC Web


Application?

Conventional-based routing in ASP.NET Core MVC defines URL patterns


and maps them to controller actions based on conventions rather than
explicitly specifying routes on each action or controller. Conventional-
based routing follows a set of conventions to map incoming requests to
specific controller actions. It is configured globally in the Program.cs file
using the MapControllerRoute middleware. This method allows for
centralized route configuration, making it easy to manage and understand
how URLs map to controllers and actions.

What is Attribute-Based Routing in ASP.NET Core MVC Application?

Attribute-based routing in ASP.NET Core MVC allows developers to define


routing directly on controller actions or at the controller level using
attributes (decorators). This approach provides more control and flexibility
over how URLs are mapped to actions compared to conventional routing,
where routes are configured globally in the Program.cs file. This approach
is useful for APIs and scenarios where URLs need to be explicitly defined
and customized.
Understanding Routing in ASP.NET Core MVC Application:
First, create an ASP.NET Core Application using the ASP.NET Core Model-
View-Controller Template. To create an ASP.NET Core Web Application
with the Model-View-Controller Project template. First, open Visual Studio
2022 and click the Create a new project tab, as shown in the image below.

Once you click on the Create a new Project tab, the following Create a new
Project window will open. From this window, select C#, All Platforms, and
Web from the respective dropdowns, as highlighted below. Select ASP.NET
Core Web App (Model-View-Controller), as highlighted below, and click the
Next button, as shown in the image below.

Once you click on the Next button, it will open the Configure Your New
Project window. Here, you need to provide the necessary information to
create a new ASP.NET Core project. First, give an appropriate name for
your project (RoutingInASPDotNetCoreMVC), set the location where you
want to create this project, and the solution name for the ASP.NET Core
Web application. And finally, click on the Create button, as shown in the
image below.

Once you click the Create button, the following Additional Information
window will open: Please select Framework .NET 8.0 (Long-term
support) and Authentication type None. You must also check
the Configure for HTTPS and do not use top-level statements check
boxes. Finally, click the Create button, as shown in the image below.

Once you click the Create button, the project will be created using the
Model-View-Controller template with the following folder and file
structure.
Modifying the Home Controller:

Once you have created the Project, next modify the Home Controller as
follows:

In the ASP.NET Core MVC Web Application, the controller action method
handles incoming HTTP Requests, i.e., URLs. For example, if we issue a
request to the /Home/Index URL, then the Index action method of the
Home Controller class will handle the request, as shown in the image
below.
Similarly, if we issue a request to the /Home/Details/2 URL, then the Details
action method of the Home Controller class will handle the request, as
shown in the image below. Here, the parameter value 2 is automatically
mapped to the id parameter of the Details action method.

Now, the question that should come to your mind is, we have not explicitly
defined any routing rules for our application. Then how is this mapping
done, i.e., how is the /Home/Index URL mapped to the Index action
method, and how is the /Home/Details/2 URL mapped to
the Details action method of the Home Controller class? This is done by
the MVC Routing Middleware Component, which is registered into the
Request Processing Pipeline.

Understanding Conventional-Based Routing in ASP.NET Core MVC


Web Application:
As we created the project with a Model-View-Controller template, the
required MVC Services and MVC Middleware components are, by default,
added to the Request Processing Pipeline. Also, the Required Routing
Middleware Components are added to the Request processing pipeline. So,
if you open the Program class, you will find the following code.
As you can see in the above code, MVC services are added by the following
statement.
builder.Services.AddControllersWithViews();

Routing Middleware Components are added using


the UseRouting and MapControllerRoute methods. If you notice the
MapControllerRoute, you will see the following.

Understanding How Routing Works in ASP.NET Core MVC Application


Registration of Route Templates (MapControllerRoute):
The MapControllerRoute method is invoked during the application startup
process to define the route templates and their corresponding patterns. It
registers all the route templates into the route table once when the
application starts. MapControllerRoute does not execute for each incoming
request. Instead, it only runs during the application startup to populate the
route table with the defined route templates.

MapControllerRoute implicitly sets up the endpoint middleware (i.e.,


UseEndpoints) necessary for handling the routes. The UseEndpoints
middleware is responsible for executing the corresponding endpoints
when a matching request comes in. So, MapControllerRoute sets up the
routes once at startup and registers the UseEndpoints middleware, which
executes the corresponding endpoint for each request that matches a route.

Routing Middleware (UseRouting):


The UseRouting middleware examines incoming HTTP requests and
matches them against the route template stored in the route table to
determine the appropriate routes. It runs for each incoming HTTP request.
When a request arrives, the UseRouting middleware parses the URL and
matches it against the defined route templates stored in the route table. If a
match is found, it then extracts values (like controller name, action method
name, and parameters) and stores them in the HttpContext under
RouteData. If no match is found, it will return a 404 error to the client.

Determining the Controller and Action (UseEndpoints):


After UseRouting identifies a matching route and prepares the necessary
context (like controller, action, and parameters), control is passed to the
UseEndpoints middleware. This is where the actual decision to execute a
specific controller action is made. UseEndpoints evaluates the route data
provided by UseRouting and invokes the appropriate controller and action
method.

Who Decides Which Action Method Executes:


The decision of which action method of which controller to execute is
primarily made by the UseEndpoints middleware, which uses the route
data populated by UseRouting. Based on the matched route and the
parameters provided in the URL, UseEndpoints routes the request to the
specific controller action that matches the criteria defined in the route
table.
Understanding the Default Route in ASP.NET Core MVC:
The default route is created with the following URL Pattern. So, if we don’t
specify anything in the URL, then by default, the Index action method of the
Home Controller class will handle the request.
{controller=Home}/{action=Index}/{id?}

In this example, the MapControllerRoute method defines a default route.


The pattern parameter specifies the route template,
where {controller}, {action}, and {id} are placeholders for route
parameters. The name parameter gives the route a name, which can be
used for generating URLs. The meaning of the placeholders are as follows:
 {controller}: Represents the name of the controller class.
 {action}: Represents the action method name within the controller.
 {id?}: Represents an optional route parameter called “id”.

With this configuration, an incoming URL like /Home/Index would match


the Index action method of the HomeController.
Similarly, /Products/Details/5 would match the Details action method of
the ProductsController with an id parameter set to 5.

Understanding Route Template in ASP.NET Core MVC Application:


The default route is created with the URL
Pattern: {controller=Home}/{action=Index}/{id?}
As you can see in the default route
template {controller=Home}/{action=Index}/{id?}, we have a question
mark at the end of the id parameter, which makes the id parameter
optional. That means the following two requests now map to the same
Details action method of the Home Controller class.
/Home/Details/1
/Home/Details
In the default route template {controller=Home}/{action=Index}/{id?},
the value Home in {controller=Home} is the default value for the
Controller. Similarly, the value Index in {action=Index} is the default value
for the action method. That means if we navigate to the application’s root
URL, as shown below, then that request will be handled by default by the
Index action method of the Home Controller class.
http://localhost:52190/

The following two URLs are also mapped to the Index action method of the
HomeController class.
http://localhost:52190/Home
http://localhost:52190/Home/Index
The default route works fine for most of the ASP.NET Core MVC Web
Applications. For example, create a Controller
named StudentController and copy and paste the following code into it.

Now, the URL /student/index is mapped to the Index() action method of


the StudentController class, and the
URL /student/details or /student/details/5 both are mapped to
the Details(int? id) action method of the StudentController.

Configure Multiple Conventional Routing in ASP.NET Core MVC


Application
In ASP.NET Core MVC, we can define multiple conventional routes to
handle various URL patterns in our application. This allows us to create
more flexible and maintainable routing configurations. This approach
allows you to direct incoming requests to various controllers and actions
based on specific URL structures. For example, we want to access the Index
Action Method of the Student Controller using the following URL.
https://localhost:44359/Student/All
To achieve this, we can configure the MapControllerRoute method, as
shown in the image below. Here, you can see we have specified the pattern
as Student/All and the default controller and action name as controller =
Student, action = Index.

Next, we want to access the Details of the action method for the Student
Controller using the following URL.
https://localhost:44359/StudentDetails/10
To achieve this, we can configure another MapControllerRoute method, as
shown in the below image. Here, you can see we have specified the pattern
as StudentDetails/{ID} and specified the default controller and action
name as controller = Student”, action = Details.

We need to access the rest of the controllers and actions using the
following URL Pattern. We also need to configure the default controller and
action names as Home and Index.
https://localhost:44359/{Controller Name}/{Action method Name}
To achieve this, we can configure another MapControllerRoute method, as
shown in the below image. Here, you can see we have specified the pattern
as {controller}/{action}/{id:int?} and specified the default controller and
action name as controller = Home, action = Index.

The complete code of the Program class is given below.


With the above changes in place, run the application and navigate to the
specific URLs, and you will get the data as expected.

Note: Routes are evaluated in the order they are defined. Ensure the most
specific routes are defined first, as the request will be handled by the first
route it matches. It’s a good practice to give each route a unique name. This
helps when generating URLs based on route names. You can specify
controller, action, and parameter default values. This is useful for defining
fallbacks for missing parts of the route.

Benefits of Conventional Routing in ASP.NET Core MVC:


 Simplicity: It reduces the need to define routes explicitly for every
action method or controller; instead, it uses default conventions.
 Predictable URLs: This feature encourages consistent URL patterns
across the application, making it easier to understand and debug
routing behavior.
 Centralized Configuration: All routing rules can be managed in one
place (Program.cs), promoting maintainability and consistency.
 Ease of Maintenance: As the application grows, maintaining a
consistent routing structure becomes easier, especially when new
controllers and actions are added.

Conventional-based routing is suitable for most ASP.NET Core MVC


applications, where the URL structure follows predictable patterns based
on controller names, action methods, and parameter names. It provides a
straightforward way to handle typical routing scenarios without the need
for extensive attribute routing.
Attribute Routing in ASP.NET Core MVC Application

What is Attribute Routing in ASP.NET Core MVC?


Attribute Routing gives us more control over the URIs by defining routes
directly at action methods or controllers in our ASP.NET Core MVC Web
Application. With Attribute Routing, you can now define routes directly on
actions and controllers by decorating them with attributes like [Route],
[HttpGet], [HttpPost], etc.

This type of routing makes it easier to understand the routes by looking


directly at the controllers and actions.

Note: The Convention-Based routing is still fully supported by ASP.NET


Core MVC. In fact, we can use both these routing approaches in the same
project. If both Routings are present, then .NET Core overrides
conventional routes.

This is because .NET Core gives preference to

Attribute Routing:

Why do we need Attribute Routing in ASP.NET Core MVC Applications?


Let us understand the need for Attribute Routing in the ASP.NET Core MVC
Web Application with an example. We will work with the example we
created in our routing in ASP.NET Core MVC articles.

Adding Student Model:


First, add a class file named Student.cs, within the Models folder, and copy
and paste the following code.

Modifying Student Controller:


We have already created the Student controller within the Controllers
folder. If not, please right-click on the Controllers folder and add a new
ASP.NET Core MVC Empty controller named StudentController.cs, and
then copy and paste the following code.
Modifying Program.cs Class file:
Next, modify the Main method of the Program class as follows. This is the
default class file you will get when creating a new ASP.NET Core Web
Application using the Model-View-Controller Project template. Here, we
have configured the MVC Middleware Component and the Convention-
Based Routing.
Now, you can access the three action methods of the Student Controller
using the following URLs, and it will work as expected.
https://localhost:44359/Student/GetAllStudents
https://localhost:44359/Student/GetStudentByID?studentID=1
https://localhost:44359/Student/GetStudentCourses?studentID=1
In some scenarios, Convention-Based routing makes it very difficult to
support certain URL patterns. However, attribute routing in the ASP.NET
Core MVC application can easily achieve those URL patterns.

In our example, if we want the URL Pattern “/student/1/courses” and if


that URL Pattern should be mapped to GetStudentCourses(int
studentID), then this type of URL Pattern is very difficult to create using
convention-based routing in an ASP.NET Core MVC Application. However,
we can easily achieve this type of URL pattern by using attribute routing in
an ASP.NET Core MVC application.
How Do We Use Attribute Routing in ASP.NET Core MVC?
Attribute routing allows us to define routes directly on our controller and
action methods using the Route Attributes. Applying the Route attribute at
the Controller level applies to all the controller’s action methods. We can
also use the [HttpGet], [HttpPost], [HttpPut], [HttpDelete], and
[HttpPatch] Attributes at the action method level to define the Route
Pattern.

Let us first modify the Student Controller as shown below. Here, we have
applied the Route Attribute at the Action methods level.
With the above Attribute Routing in place, we can now access the above
three action methods for the student controller using the following URLs.
https://localhost:44359/Student/All
https://localhost:44359/Student/1/Details
https://localhost:44359/Student/1/Courses

Can we Apply Multiple Route Attributes to a Single Action Method in


ASP.NET Core MVC?
Yes, we can apply Multiple Route Attributes to a Single Action Method in
the ASP.NET Core MVC Application. Let us understand this with an
example. Please modify the Home Controller as shown below, and please
have a look at the Index Action method, where we have applied three
attributes.

If you notice, we have applied the Route attribute three times to the
Index() action method of the Home Controller class. With each instance of
the Route attribute, we specified a different route template. With the above
three Route attributes applied to the Index action method, we can now
access the Index() action method of the HomeController using the following
three URLs.

https://localhost:44359/
https://localhost:44359/home
https://localhost:44359/home/index

If you apply the same Route Attributes multiple times with an Action
Method and if you apply the same Route Template to different action
methods, then you will get AmbiguousMatchException. Now run the
application and navigate to the above three URLs, and you will see the
Home page as expected.
Attribute Routing with Parameters in ASP.NET Core MVC Application:
As we already discussed, we can specify the Route Parameters as part of
the route template with Conventional-Based Routing. We can also do the
same with Attribute Routing in ASP.NET Core. That means we can also
define Route Attributes with Route Parameters. To understand this, modify
the Home Controller as shown below.

As you can see in the above code, the Details() action method has the id
parameter. Notice that in the route template, we also specified the ID
parameter ([Route(“Home/Details/{id}”)]). So, the URL
(/Home/Details/10) will execute the Details(int id) action method and
map the value 10 to the id parameter of the Details action method. This is
done through the model binding process, which will be discussed in our
upcoming articles. Run the application and navigate to the following URL;
you should see the output as expected.
https://localhost:44359/Home/Details/10

Attribute Routing with Optional Parameters in ASP.NET Core MVC


Application:
We can also make the Route Parameter optional in Attribute Routing. You
can define a Route Parameter as optional by adding a question mark (?) to
the route parameter. You can also specify the default value by using
the parameter = value.

In our example, the Details(int id) action method of the HomeController is


currently executed only if we pass the id parameter value. If we do not pass
the id parameter value, we will get a 404 error. For example, if we navigate
to the following URL, we will get a 404 error.
https://localhost:44359/Home/Details

Let us modify the Route attribute of the Details action method as shown
below to make the route parameter id optional by adding “?” at the end,
i.e. [Route(“Home/Details/{id?}”)].

Now run the application and navigate to the following URL. You will see the
output as expected instead of a 404 error.
https://localhost:44359/Home/Details
We can also make the parameter optional by specifying a default value. For
a better understanding, please modify the Home Controller class as follows.
Within the Route Attribute of Details action method, we have specified the
default value for the id parameter as 10.
If you run the application and navigate to the URL below without specifying
the ID parameter, it will take the default value 10.
https://localhost:44359/Home/Details

Controller and Action Method Names in Attribute Routing:


With Attribute Routing in ASP.NET Core MVC Application, the Controller
and Action Method names do not play any role. To understand this, modify
the Home Controller as shown below.

As you can see, we have specified the Route attribute three times with the
StartPage() action method of the HomeController. So, this StartPage action
method will be executed for the following 3 URLs.
https://localhost:44359/
https://localhost:44359/home
https://localhost:44359/home/index

Can we use both Attribute and Convention-Based Routing in a Single


ASP.NET Core MVC Application?
Yes. Both routing mechanisms can be combined in a single ASP.NET Core
MVC Web Application. The controller action methods with the [Route]
attribute use Attribute Routing, and those without the [Route] attribute use
Convention-based routing. To better understand, please modify the Home
Controller class as shown below.
As you can see in the above code, we have applied the Route Attribute with
the Start Page action method, and we have not applied the Route Attribute
with the Privacy action method. So, in this case, StartPage will use Attribute
Routing, and we can access this method using the following 3 URLs.

https://localhost:44359/
https://localhost:44359/home
https://localhost:44359/home/index

On the other hand, the Privacy action method will use Contention-Based
Routing and can be accessed by using the following URL only.
https://localhost:44359/home/privacy

Note: It is not possible to access an action method using both Attribute and
Convention Routing. If Attribute Routing is applied to an action method,
then you can access that method using Attribute Routing only; you cannot
access the same method using Convention Routing. Similarly, if Attribute
Routing is not applied to an action method, then you can access that
method using Conventional Routing only; you cannot access the same
method using Attribute Routing.

Applying Route Attribute at the Controller Level in the ASP.NET Core


MVC:
In the ASP.NET Core MVC Web Application, applying the Route() Attribute
to the Controller class and individual action methods is possible. If you
want to make attribute routing less repetitive, you need to use the Route
Attribute on the controller level.

Let us understand this with an example. First, modify the Home Controller
class, as shown below. Here, we have created the Home controller with two
action methods.
With the above code in place, we can access the Index() action method
using the following 3 URLs.

/
/Home
/Home/Index

Along the same line, we can access the Details(int? id) action method
using the following 2 URLs.

/Home/Details
/Home/Details/2
If you look at our code, you will see that we are using Route Attributes at
the action method level to define routes. Further, you will notice that all the
routes in the HomeController start with the same prefix, i.e., Home. That
means Home is the common prefix for all the routes in the Home
Controller.

Here, instead of writing the common prefix Home at each action method,
we can specify the Home for the Home Controller (for all its action
methods) using the [Route] attribute at the controller level.

So, modify the Home Controller class as follows. Here, you can see we have
applied [Route(“Home”)] at the controller level, and we have removed
that Home prefix from each action method Route Attribute. The Route
Attribute we define at the Controller level will be applied to each action
method.
The Route template applied on the controller level is prepended to the
route template applied to the action method level. When you navigate to
the following four URLs, you will get the output as expected.

https://localhost:44359/home
https://localhost:44359/home/index
https://localhost:44359/home/details
https://localhost:44359/home/details/10

However, when you navigate to the application’s root URL


(http://localhost:44359), you will get a 404 error. To solve this, you need
to include the route template that begins with “/” i.e. [Route(“/”)] on the
Index() action method, as shown in the image below.

So, modify the Home Controller as follows:


With the above changes in place, run the application and navigate to the
root URL to see the expected output. As you can see in the above code, we
have applied the [Route(“Home”)] attribute at the Controller level. This
Route attribute eliminates the need to repeat the common prefix “Home”
on each controller action method. However, sometimes, we may need to
override the route attribute defined at the controller level.

How Do We Ignore the Route Attribute Placed at the Controller Level


in ASP.NET Core MVC?
To ignore the Route Template placed at the Controller level, you must use /
or ~/ at the action method level. If the action method route template starts
with / or ~/, then the controller Route Attribute will not be combined with
the action method route attribute. To understand this, let us modify the
Home Controller class as shown below. In the following code, the About
action method starts with ~/, so this action method will not be combined
with the controller route attribute.
Now run the application and navigate to /About URL, and you will see the
output as expected.

Defining Route using HTTP Methods:


We can also achieve this using HTTP Verbs. So, let us rewrite the previous
example using HTTP Methods as follows:
What is the Difference Between [Route] and [HttpGet], [HttpPost], etc.,
Attributes?
The [Route] attribute specifies a route template for a controller or action
method but does not apply any specific HTTP method. On the other hand,
HTTP method attributes like [HttpGet], [HttpPost], [HttpPut], and
[HttpDelete] not only define the route template but also restrict the action
method to be invoked only with the specified HTTP method.

Tokens in Attribute Routing:


Tokens in Attribute Routing allow you to dynamically replace parts of the
route template. These tokens are placeholders within route templates that
are substituted with actual values during URL generation and routing. In
ASP.NET Core MVC, Tokens in Attribute Routing are enclosed in square
brackets [] and can represent values such as controller name, action name,
etc.

The following are the Commonly Used Tokens in ASP.NET Core MVC:
 [controller]: This represents the controller’s name without the
“Controller” suffix. For example, a controller named HomeController
would have the [controller] token substituted with Home.

 [action]: Represents the name of the action method. For example, an


action method named Index would mean the [action] token gets
substituted with Index.

Token Replacement Example in Attribute Routing:


Let us understand Token Replacement in ASP.NET Core Attribute Routing
with an example. Please modify the Home Controller class as shown below.
Here, we are applying the token [controller] to the Home Controller and
the token [action] to all the action methods.
With the above controller and action tokens in place with the Route
Attribute, you can now access the Index action method of the Home
Controller with the URL /Home/Index. Similarly, you can access the
Details action method of the Home Controller using the
URL /Home/Details.

 The route for the Index action method would


be /Home/Index because of the [controller] and [action] tokens
used in the controller’s route attribute.

 The route for the Details action method would


be /Home/Details because of the [controller] and [action] tokens
used in the controller’s route attribute.

Now, run the application and see if everything is working as expected. The
token controller will be replaced with the actual controller name, and
similarly, the token action will be replaced with the actual action method
name.

How Do We Make the Index Action Method the Default Action?


With the controller and action tokens in place with Route Attribute, if you
want to make the Index action method of Home Controller the default
action, then you need to include the [Route(“/”)] attribute with an “/” on
the Index action method. So, modify the Home Controller as shown below
and rerun the application. It should display the Index Action method.
Do We Need to Write the Action Token for Each Action Method?
Not Really. If you want all your action methods to apply the action token,
then instead of applying the [action] token on each and every action
method, you can apply it only once on the controller.

So, in this case, the Route attribute at the controller level should include
both controller and action tokens like [Route(“[controller]/[action]”)].
So, modify the Home Controller class as shown below to apply the
controller and action tokens at the controller level.

Now, run the application, and it should work as expected.


Advantages of Using Tokens in ASP.NET Core Attribute Routing:
Attribute routing with tokens provides a flexible and expressive way to
define dynamic routes in your ASP.NET Core MVC application.

The following are the advantages of using Token with Attribute Routing in
ASP.NET Core Web Applications:

 Reduction of Redundancy: Tokens help reduce redundancy by


eliminating the need to repeat the controller or action names in each
route definition.

 Maintainability: Changing the name of a controller or action only


requires updating the class or method name, not the routes.

 Clarity: Route templates with tokens are generally easier to read and
understand, as they directly relate to the controller and action names.
ASP.NET Core MVC – Data Passing Techniques
 ViewData in ASP.NET Core MVC Application
 ViewBag in ASP.NET Core MVC
 TempData in ASP.NET Core MVC

ViewData in ASP.NET Core MVC Application


How do you Pass Data to Views in ASP.NET Core MVC?
In ASP.NET Core MVC Applications, we can pass the data from a controller
action method to a view in many different ways, such as by
using ViewBag, ViewData, TempData, and a Strongly Typed Model.

The other three techniques, i.e., ViewBag, Tempdate, and Strongly Typed
View Model, will be discussed in our upcoming articles.

What is ViewData in ASP.NET Core MVC Application?


ViewData is a dictionary object in ASP.NET Core MVC that allows us to pass
data from a controller action method to a view. It provides a flexible way to
pass data, allowing us to use key-value pairs. ViewData is useful when we
need to pass dynamic data or data that doesn’t fit well into a strongly typed
model.

If you go to the definition of ViewData, you will see that It is a property in


the Controller abstract class, and its type is ViewDataDictionary. This
property is also decorated with the ViewDataDictionary attribute, as shown
in the image below.

As you can see in the above image, the data type of ViewData Property is
ViewDataDictionary. Let’s look at the definition of the ViewDataDictionary
by right-clicking on it and selecting go to definition, and you will see the
following definition.

As you can see in the above image, the ViewDataDictionary class


implements the IDictionary. So, ViewData in ASP.NET Core MVC is a
dictionary object. As a dictionary object, it stores data in the form of Key-
Value Pairs, where each key must be a string, and the value that we pass to
the dictionary will be stored as an object type. Here, the key is of type
String, and the value is of type object, i.e., we can store any data, including
null.

How do you use ViewData in the ASP.NET Core MVC Application?


To use ViewData in the ASP.NET Core MVC Application, we need to set the
data in a controller by adding entries to the ViewData dictionary with a
string key and then assign some data. The key should be in string format,
and you can give any name to the key. Then, you can assign any data to this
key, such as the following.

ViewData[“KeyName”] = “Some Data”;

Since ViewData is a server-side code, hence to use it on a view, we need to


use the razor syntax, i.e., @ something as follows.

@ViewData[“KeyName”]

You can access the string data from the ViewData dictionary without
casting the data to string type. But if you are accessing data other than the
string type, you must explicitly cast the data to the type you expect.

How to Access String Data using ViewData in ASP.NET Core MVC

How to Access Student Data using ViewData in ASP.NET Core MVC


Example to Understand ViewData in ASP.NET Core MVC Application:
Let us see an example of using ViewData to pass data from a controller
action method to a view. In our example, we want to pass three pieces of
information from the controller action method to the view. One is the
Page’s title, the second is the page’s Header, and the third is the Student
data.
So, first, create an ASP.NET Core Web Application (with the
name FirstCoreMVCWebApplication) with MVC (Model-View-Controller)
Project Template, i.e., Creating ASP.NET Core Application using ASP.NET
Core Web APP (Model-View-Controller) Template. Once you create the
project, add a class file named Student.cs in the Models folder. Then, please
copy and paste the code below into it.

Modifying HomeController:
Next, modify the Home Controller class as shown below. Here, we remove
the existing code and add one action method, i.e., Details. As part of this
method, we create three ViewData objects to store the Title, Header, and
Student data. Here, Title, Header, and Student are the keys of the ViewData
Dictionary Object.
Creating Details.cshtml View:
In our previous article, we discussed the different ways to create Views in
ASP.NET Core MVC Applications. Let us add a view
named Details.cshtml within the Home folder, which is inside
the Views folder, as shown below. Once you add the Details view, your
Views folder structure should look as shown below.

Now open the Details.cshtml view and copy and paste the following code.
As you can see in the code below, we directly access the string data from
the ViewData without type casting. However, while accessing the Student
data from the ViewData, we typecast it to the appropriate Student type.

Now run the application and navigate to the “/Home/Details” URL. As


shown below, you will see the data as expected.
Features of ViewData in ASP.NET Core MVC:
 Type Safety: ViewData does not provide type safety as it stores data
as an object. You need to cast the data to the appropriate type when
retrieving it.
 Scope: The data in ViewData only lasts for the duration of the current
HTTP request. It’s reset for each new request, so it is not a
mechanism for persisting data between requests, such as across
redirects.
 Accessing Data: You can set data in a controller by adding entries to
the ViewData dictionary with a string key. In the view, you can access
these values using the same key. When used in the view, the values
must be cast to the appropriate type.
 Null Handling: ViewData returns null if a key does not exist, so you
should check for null or use the ?. operator to avoid exceptions.
 Usage: ViewData is best used to pass small amounts of data. If you’re
passing complex data or require type safety, consider using ViewBag
or strongly typed views.
 Lightweight: ViewData is lightweight because it’s just a dictionary
and doesn’t require any extra classes or interfaces.
 Dynamic: ViewData is a dynamic object, so you can use it to pass any
data.
So, we need to use ViewData when we need to pass simple or small
amounts of data that don’t justify creating a view model or when the data is
temporary and used only within the view being rendered.
ViewBag in ASP.NET Core MVC Application

In the ASP.NET Core MVC application, we can pass data from a controller to
a view using ViewData, ViewBag, TempData, and a Strongly Typed View
Model.

What is ViewBag in ASP.NET Core MVC?


In ASP.NET Core MVC, ViewBag is a dynamic object that provides a way to
pass data from the controller action method to the view. It allows us to
create properties dynamically on the fly and add properties to the ViewBag
object in the controller actions, which are then accessible in the Razor
views. ViewBag is useful for passing simple data (e.g., strings, dates) that do
not require a strongly typed view model. If you go to the Controller abstract
class, you will find the following signature of the ViewBag property.

So, in the ControllerBase class, which is the base class for controllers in
ASP.NET Core MVC, as you can see, there’s a property called ViewBag,
which is a type of DynamicViewData. This DynamicViewData is a dynamic
object that allows properties to be dynamically added at runtime.
Note: The dynamic type is introduced in C# 4.0. It is very similar to the var
keyword, which means we can store any value in it, but the type will be
decided at run time rather than compile time.

How Do We Pass and Retrieve Data from ViewBag in ASP.NET Core


MVC?
So, when we set a property on ViewBag in a controller action,
like ViewBag.Header= “Student Details”; DynamicViewData internally
adds or updates a property with the name “Header” and the
value “Student Details” like dynamic Header = “Student Details”. Based
on the right-hand side value, the data type will be decided at runtime. For
example, if the value is a string, the data type will be considered a string at
runtime.
In a Razor view file (.cshtml), we can access the properties set in ViewBag
directly using the @ symbol followed by the property name
(@ViewBag.Header ). This dynamic access is possible because ViewBag is
of type dynamic.
How to Access Data from ViewBag in ASP.NET Core MVC with String Type:

How to Access Data from ViewBag in ASP.NET Core MVC with Complex
Type:

Since ViewBag is dynamic, accessing a property that doesn’t exist or is not


set will not throw a compile-time error. Instead, it will result in a runtime
NullReferenceException if the property is accessed without being set.

The data stored in ViewBag is available only during the current request
lifecycle. Once the view is rendered and sent to the client, ViewBag data is
no longer accessible.

Because ViewBag uses dynamic, there is no compile-time type checking. If


property names are mistyped or changed without updating all references,
this can lead to potential runtime errors.
Example to Understand ViewBag in ASP.NET Core MVC Application:
Example of using ViewBag to pass data from a controller action
method to a view in the ASP.NET Core MVC Application.

We will work with the same example we worked on in our previous article
with ViewData. So, modify the Details action method of the HomeController
class as shown below.

As you can see in the above example, here we are using the dynamic
properties Title, Header, and Student on the ViewBag.
Accessing ViewBag in a View in ASP.NET Core

Now, we will see how to access the ViewBag data within a View in MVC
Application. So, modify the Details.cshtml view file as shown below. Here,
you can see we are directly accessing the string and complex type values
without typecasting, and this is possible because of the dynamic data type.
As you can see, we are accessing the data from the ViewBag using the same
dynamic properties: Title, Header, and Student. Now run the application
and navigate to the “/Home/Details” URL, and you will see the data as
expected on the webpage, as shown in the image below.

When should you use ViewBag in ASP.NET Core MVC?


Here are some scenarios when you might use ViewBag:
 Passing Simple Data to Views: If you need to pass simple data to a
view, like Page Titles, Headers, or small messages, ViewBag can be a
straightforward choice.
 Displaying Messages or Notifications: This is used for sending
feedback messages to a view, such as success, error, or information
messages after form submissions.
Differences Between ViewBag and ViewData in ASP.NET Core MVC
In ASP.NET Core MVC, both ViewBag and ViewData are mechanisms to pass
data from controllers to views, but they have some key differences in how
they work and their usage:

Type Safety:
 ViewBag: It uses dynamic to store data, so there’s no compile-time
type checking. This means you can set and access properties
dynamically, but you may encounter runtime errors if the property
name is mistyped or if the types don’t match.
 ViewData: It uses a Dictionary<string, object> to store key-value
pairs. While not type-safe at compile-time, you have to cast values
when retrieving them ((string)ViewData[“Message”]). This provides
slightly more safety compared to ViewBag.

Usage and Syntax:


 ViewBag: Accessed using dot notation (@ViewBag.Message) in Razor
views. Properties are set directly (ViewBag.Message = “Hello”;) in the
controller actions.
 ViewData: Accessed using a key (@ViewData[“Message”] as string) in
Razor views. Properties are set using Key-Value Pait to ViewBag
(ViewData[“Message”] = “Hello”;) in controller actions.

Scope and Lifetime:


 Both ViewBag and ViewData are available during the current request.
They are typically used to pass data from controllers to views within
the same request lifecycle.

Error Handling:
 ViewBag: Due to its dynamic nature, errors like null references or
type mismatches might occur at runtime if not handled carefully.
 ViewData: Requires explicit casting when retrieving values, which
can help catch type mismatches earlier.
When to Use:
 ViewData: We need to use ViewData when working within loops or
conditional logic where dictionary operations are more
straightforward.

 ViewBag: We need to use ViewBag when we want cleaner syntax and


are dealing with a very small amount of data that does not require
type-checking.
What is a Strongly Typed View in ASP.NET Core MVC?
A Strongly Typed View in ASP.NET Core MVC is associated with a specific
class, known as a view model. This class defines the data and behaviors the
View needs to display or interact with. It is a view that expects a specific
data model to be passed to it from the controller action method.

Why do we need a Strongly Typed View in ASP.NET Core MVC?


As we already discussed, in ASP.NET Core MVC, we can pass the model data
to a view using many different methods, such as ViewBag, ViewData,
TempData, Strongly Typed View Model, etc. The view becomes loosely
typed when we pass the model data to a View using ViewBag, TempData, or
ViewData. We will not get intellisence support or compile-time error
checking in a loosely typed view.

With a Strongly Typed View, we will get the following benefits:


 Type Safety: By specifying the model type, we get compile-time type
checking, which helps prevent runtime errors due to type
mismatches. With a strongly typed view, the compiler checks that the
view is using properties and methods that exist on the view model
class.
 Intellisense Support: IDEs like Visual Studio provide intellisense for
the model properties, which provides auto-completion of model
property names, improving development speed and reducing errors.
 Refactoring Support: Refactoring becomes easier and safer; for
example, renaming a property on a model will automatically indicate
places in views where changes are needed.
How Do We Create Strongly Typed Views in ASP.NET Core MVC?
To create a strongly typed view in ASP.NET Core MVC, we need to follow
the below steps:
1. Create a Model: Define a model class representing the data you want
to display in the view. This class contains properties corresponding
to the data you need to access and render in the view.
2. Pass the Model to the View: In the controller action method, create
an instance of the model class, populate it with data, and then pass it
to the view.
3. Declare the Model in the View: At the top of the view file, declare
the model type using the @model directive. This informs the view
about the type of data it will be working with.
4. Access Model Properties in the View: You can access the model’s
properties inside the view using Razor syntax
(@Model.PropertyName). Here, you will get the intellisence
support for the model properties.

Example to Understand Strongly Typed View in ASP.NET Core MVC


Application

Create a Model
First, you need to define a model class. A model is a C# class with
properties. This class will be used to transfer data between your controller
and view. We will use the same application we used in our previous two
articles. So, we are going to use the same Student model. If you are coming
to this article directly without reading our previous two articles, then
please create the following Student class within the Models folder:

To create a Strongly Typed View from the controller’s action method, we


must pass the model object as a parameter to the View() extension method.
The Controller base class provides the following two overloaded versions
of the View() extension method, which we can use to pass the model object
from the controller action method to a view in the ASP.NET Core MVC
Application.
Now, we will use the overloaded version of the Views() Extension method,
which takes only the model object as an input parameter. So, modify the
Details action method of the Home Controller, as shown below, to pass the
student object as a parameter to the View extension method.

Changes in Details.cshtml View:


To create a strongly typed view in the ASP.NET Core MVC Application, we
need to specify the model type within the view using the @model directive.
Here, the Student class will be our model, passed from our action method
as a parameter to the View Extension method. So, we need to specify the
model for the view, as shown below.
@model FirstCoreMVCApplication.Models.Student

The above statement will use


FirstCoreMVCApplication.Models.Student as the model for this view.

In the directive (@model), m is in lowercase, and the statement should


not be terminated with a semicolon.
Then, to access the model object properties, you can use @Model.

Here, the letter M is in uppercase. So, in our example, we can access the
Student objects properties such as Name, Gender, Branch, and Section by
using @Model.Name, @Model.Gender,@Model.Branch,
and @Model.Section, respectively.

So, modify the Details.cshtml view file is shown below to make the view a
Strongly Typed View.

Now run the application and navigate to the /Home/Details URL. As


shown in the image below, you will see the data as expected.
In our example, we still use ViewBag and ViewData to pass the Header and
Title from the Controller action method to the View.

Then definitely, the question that comes to your mind is how we will pass
the Header and Title to a strongly typed view.

In such scenarios, we need to use a view-specific model called View Model.

ViewModel in ASP.NET Core MVC Application

What is a ViewModel in ASP.NET Core MVC?

A View Model in ASP.NET Core MVC is a class that represents the data and
logic required by a view. It is specifically designed to serve the needs of the
user interface and does not necessarily map directly to the domain model
or database entities.

The primary purpose of a View Model is to encapsulate all the data that the
view needs to render, making it easier to pass this data from the controller
to the view. It acts as an intermediary between the controller and the view,
encapsulating the data displayed or edited on the view.

In Real-Time Applications, a single model object may not contain all the
data required for a view. We must use a View Model in the ASP.NET Core
MVC application in such situations. In simple words, a View Model in
ASP.NET Core MVC is a model that contains more than one model data
required for a particular view.

How Do We Implement the View Model in an ASP.NET Core


Application?
To implement a view model in ASP.NET Core MVC, follow these steps:
 Define the View Model: Create a class that includes properties
relevant to the view’s needs.
 Populate the View Model in the Controller: In your controller,
populate the view model from the domain model or other data
sources.
 Pass the View Model to the View: Pass the populated view model to
the view from the controller using the View overloaded method,
which takes the model object as a parameter.
 Use the View Model in the View: Access the view model’s
properties in the view using Razor syntax.
Let us understand the View Model in ASP.NET Core MVC with an Example.
The following diagram shows the visual representation of a view model in
the ASP.NET Core MVC application.

Let’s say we want to display the student details in a view. We have two
different models to represent the student data. The Student Model is used
to represent the student’s basic details, whereas the Address model is used
to represent the student’s address.

Along with the above two models, we also required some static information
in the view, like the page header and page title. If this is our requirement,
then we need to create a view model, say StudentDetailsViewModel, and
that view model will contain both the models (Student and Address) and
properties to store the page title and page header.

Creating the Required Models:


We will use the same example that we have been worked so far. First,
modify the Student.cs class file, which is present within the Models folder
of your application, as follows. This model will represent a student’s basic
information, such as a Name, Branch, Section, etc.

Next, create the Address model, representing the Student Address, such
as City, State, Country, etc. So, create a class file with the
name Address.cs within the Models folder and copy and paste the
following code.
Creating the View Model:
Now, create the View Model to store the data required for a particular view.
In our case, it’s the student’s Details view. This View Model will represent
the Student Model + Student Address Model + some additional data like
Page Titles and Headers.

You can create the View Models anywhere in your application, but it is
recommended to create all the View Models within a folder
called ViewModels to keep things organized.

So first, create a folder at the root directory of your application with the
name ViewModels, and then create a class file with the
name StudentDetailsViewModel.cs within the ViewModels folder. Copy
and paste the following code once you create
the StudentDetailsViewModel.cs class file.

We named the ViewModel class StudentDetailsViewModel. Here, Student


represents the Controller name, and Details represents the action method
name within the Student Controller. As it is a view model, we prefixed the
word ViewModel. Although it is not mandatory to follow this naming
convention, I personally prefer to follow it to organize view models.

Creating Student Controller:


Right-click on the Controllers folder, add a new class file
named StudentController.cs, and copy and paste the following code. As
you can see in the Details action method, we populate
the StudentDetailsViewModel with the required data and then send it to
the corresponding view.

As you can see, we are now passing the view model as a parameter to the
view. This view model contains all the data required by the Details view.
We are not using any ViewData or ViewBag to pass the Page Title and
Header to the view; instead, they are also part of the ViewModel, which
makes it a strongly typed view.

Creating the Details View:


First, add a folder with the name Student within the Views folder of your
project. Once you add the Student Folder, then you need to add a Razor
view file with the name Details.cshtml within the Student folder. Once you
add the Details.cshtml view, then copy and paste the following code into it.
Now, the Details view has access to the StudentDetailsViewModel object
that we passed from the controller action method using the View()
Extension method. Using the @model directive, we
set StudentDetailsViewModel as the Model for the Details view. Then, we
access Student, Address, Title, and Header using the @Model property.

Now run the application and navigate to the /Student/Details URL. As


shown in the image below, you will see the expected output on the
webpage.
Benefits of View Model in ASP.NET Core MVC:
With View Model in ASP.NET Core MVC, we will get the following benefits:
Strongly Typed: Unlike ViewData or ViewBag, which are dynamic and
loosely typed, a ViewModel is a strongly typed object. This means it has a
specific, predefined structure with properties of defined types. This strong
typing ensures compile-time type checking, reducing the risk of runtime
errors and making the codebase more robust and maintainable.
Separation of Concerns: View models separate the presentation layer
from the business logic and data access layers. This means views are not
dependent on the database schema or domain logic, promoting cleaner and
more maintainable code.
UI Customization: They allow customization of the data presentation for
specific views without impacting the underlying data models.
Data Aggregation: View models can aggregate data from multiple domain
models or services into a single object that is easy for the view to consume.
Security: Using view models can improve security by ensuring that only
the necessary data is exposed to the view. It prevents over-posting attacks
where users could potentially submit additional, unwanted fields through
form submissions.
Optimized Data Loading: View models can help optimize data transfers
between the server and client by including only the data necessary for the
view, reducing payload sizes and load times.
TempData in ASP.NET Core MVC
 Why do we need TempData in the ASP.NET Core MVC
Application?
 What exactly is TempData in ASP.NET Core MVC?
 How Do We Use TempData in ASP.NET Core MVC?
 How do you Pass and Retrieve data from TempData?
 How do you retain TempData values in the Consecutive
Request in ASP.NET Core MVC?
 How do you Store a Complex Object in TempData in ASP.NET
Core MVC?

Why do we need TempData in the ASP.NET Core MVC Application?


The limitation of both ViewData and ViewBag is they are limited to one
HTTP request only. So, if redirection occurs, their values become null,
meaning they will lose the data they hold. In many real-time applications,
we may need to pass the data from one HTTP Request to the next
subsequent HTTP Request. Then, in such situations like this, we need to
use TempData in the ASP.NET Core MVC Application.

TempData in ASP.NET Core MVC serves the purpose of passing data from
one request to the next, specifically between actions or redirects.

The following are some of the common use cases of TempData in ASP.NET
Core MVC Applications:
 Passing Data After Redirects: TempData helps to pass data after
redirects, a common requirement in web applications where a
method performs operations and redirects to another method.
 Storing Temporary Data: It is useful for storing temporary data that
does not need to be persisted in the database but must be available
across requests.
 Message Passing: Commonly used for displaying success or error
messages on action redirects, especially in post-redirect-get patterns.

What is TempData in ASP.NET Core MVC?


TempData in ASP.NET Core MVC Application is one of the mechanisms for
passing a small amount of temporary data from a controller action method
to a view and from a controller action method to another action method
within the same controller or to a different controller. It uses session
storage to persist data across requests, ensuring data is available after a
redirect.
TempData is cleared after it is read, making it suitable for temporary data
that doesn’t need to persist beyond a single request cycle. If you go to the
Controller class, you will see the following signature for the TempData.

As you can see in the above image, the data type of the TempData is
ITempDataDictionary. Let us see the definition of the ITempDataDictionary
class.

As you can see, the ITempDataDictionary class implements the IDictionary


interface. So, we can say that TempData in ASP.NET Core MVC is a
dictionary object. As a dictionary object, it stores data in the form of key-
value pairs, where each key must be a string.

The value we pass to the dictionary will be stored as an object type. Here,
you can also see that to manage the TempData value in ASP.NET Core MVC,
it provides 5 methods.

The use of these methods is as follows:

Load and Save:


These methods are primarily used by the ASP.NET Core framework to
handle the lifecycle of TempData. Developers usually do not need to call
these methods directly.
 Load: The Load method loads the TempData values from the current
HTTP context. The framework typically invokes this method
internally, and it is not commonly used directly in application code.
 Save: The Save method is used to save the current state of TempData
into the current HTTP context. It’s typically called automatically by
the ASP.NET Core framework at the end of a request, ensuring that
any changes made to TempData during the request are persisted for
the next request.
Keep:
The Keep method marks all items in TempData to be retained for the next
request. This method is useful to ensure that TempData is not cleared after
reading.

Keep(string key):
This method retains a specific item in TempData for another request. It is
useful when we have multiple items in TempData but only need to preserve
some of them. Only the TempData entry with the specified key will be
preserved for the next request, while others will be discarded.

Peek(string key)
This method reads an item from TempData without removing it. This is
useful when we need to access the same TempData item across multiple
requests or in multiple places within the same request. Unlike reading
TempData using the String Indexer, which marks the item for deletion,
Peek allows the data to be read and still remain in TempData for further
requests.

How do you Pass and Retrieve data from TempData in ASP.NET Core
MVC?
The most important point you need to remember is that it stores the data
as an object, so while retrieving the data from TempData, type casting is
required. If you are accessing string values from TempData, typecast is not
required. However, it is mandatory to typecast explicitly to the actual type
if you are accessing data other than the string type from the TempData.

 Passing Data: Set values in TempData using TempData[“Key”] =


value; in one action.
 Retrieving Data: Access values from TempData using var value =
TempData[“Key”] as Type; in another action.

Example to Understand TempData in ASP.NET Core MVC Application


We will use the same application we have worked on so far. Please modify
the HomeController to use TempData, as shown below. As you can see, we
have created three action methods here. In the Index action method, we
have set the Name and Age values using TempData, and in the other two
action methods, we are not doing anything.
Next, modify the Index, Privacy and add the About Views within the Home
Folder, which is inside the Views folder as follows:
Index.cshtml

Privacy.cshtml

About.cshtml

The implementation of all three methods is the same. From each view, we
access the TempData values using the String Indexer, i.e., using the string
key name. Now, run the application and visit the Index Page, and you will
get the data as expected, as shown in the image below.

Now, if you visit Privacy and About, you will not get the data as expected, as
shown in the image below.

Why are we not getting the TempData value in the next subsequent
Request?

The reason is that once we read the value from TempData using the String
Indexer, it will mark the item for deletion from the TempData dictionary
collection. And this is the default behavior of TempData. In our example, we
are reading the TempData within the Index View. Hence, after reading the
Age and Name keys from TempData, these two keys will be deleted from
the TempData dictionary collection.

How Do We Retain the TempData value in the next Sub Sequent


Request in ASP.NET Core MVC?
We can retain the TempData values for the next subsequent request in
many ways. Let us discuss them one by one.

Method 1: Don’t Fetch the Data from TempData


If we don’t fetch the data from the TempData either from the Index Action
Method or from the Index View. In that case, the TempData will be
preserved in the TempData dictionary collection and can be accessed from
the next subsequent request. To do so, let us modify the Index.cshtml view
as follows.

As you can see, we are only fetching the Age key from TempData, not the
Name key, which means the Name key will be preserved in the TempData
dictionary collection for the next request.

Now, run the application, visit the Index page, and then visit either
the Privacy or About Page, as shown in the image below. You can see that
the age value is on the index page, and the name value is on the Privacy
page.
If you visit the About page, you will not see the data shown in the image
below.

Method 2: Use the Keep Method to Preserve the Data in TempData

To preserve the TempData for the next request, use the TempData
Keep or Keep(string) Method. In this case, no matter whether you access
the data or not from the TempData, it will preserve the TempData value for
the next request. The Keep() method will Mark all keys in the dictionary
for retention.

On the other hand, the Keep(string key) method marks the specified key
in the dictionary for retention. To better understand, please modify the
Home Controller as follows. Here, you can see inside the Index Action
method we call the Keep method.
Next, modify the Index.cshtml view as follows. As you can see, we are
fetching both keys from the TempData Dictionary Collection.

Now, run the application, visit the Index page, and then visit either the
Privacy or About Page, as shown in the image below. Both the Index Page
and Privacy Page show the Name and Age values.

If you visit the About page, you will not see the data shown in the image
below.
How Do We Preserve the TempData for the About Page?

In that case, we need to call the Keep method inside the Privacy Action
method. So, modify the Home Controller as follows:

Method 3: Using TempData Peek Method

The key will be deleted from the TempData Dictionary Collection by default
if we read the data using the string Indexer. So, instead of reading the
values using the string Indexer, we can also read the data from TempData
using the Peek method. To do this, we need to pass the key name to the
Keep method. In this case, it will read the data but preserve the key for the
next subsequent request.

To Understand this concept, please modify the Home Controller class as


follows. Inside the Index Action method, we store the data in TempData
and redirect the request to the Privacy Action Method. As the Index View is
not executed, we are not fetching the data from the TempData. So, these
TempData keys are available for the next request, which is, in this case, the
Privacy Action method. We are fetching the data from the Privacy action
method using the Peek method, which will return the data and keep the
keys alive for the next request. And inside the About Page, we can access
the value.
Modifying Privacy.cshtml view:
Inside the Privacy.cshtml View, we need to use ViewData instead of
Tempdata as follows:

With the above changes, run the application and see if everything works as
expected. So, when you run the application, the Index action method will be
redirected to the Privacy Action method. Then, if you visit the About action
method, you will also get the data as expected, as shown in the image
below.

How Do We Store a Complex Object in TempData in ASP.NET Core


MVC?
To store a complex object in TempData in ASP.NET Core MVC, we need to
serialize it to a string format, typically JSON, and then deserialize it when
retrieving it.

Serialize the Object to JSON


Before storing the complex object in TempData, we must convert it to a
JSON string. This is because TempData can only store primitive types. For
serialization, you can use System.Text.Json.JsonSerializer, or
Newtonsoft.Json.

The following is the syntax.

Retrieve and Deserialize the Object


When retrieving the object, get the JSON string from TempData and
deserialize it to the original object type. The syntax is as follows.
Handling TempData Persistence
If you need the data to persist for another request, you must read it and
keep it again. The syntax is as follows.

Creating the Model:


First, create the following Student class within the Models folder.

Modifying the Home Controller:


Now, run the application and test it, and it should work as expected.

How TempData Manage the Data in ASP.NET Core MVC:


ASP.NET Core MVC provides different storage mechanisms for TempData,
known as TempData providers. Depending on the configuration and
application requirements, TempData can be stored using cookies or a
session state.
 Cookie-Based TempData Provider: This provider stores TempData
using cookies. The data is serialized into JSON, encoded, and stored as
cookies. This method is stateless but limited by the size of cookies
and involves sending data back and forth between the server and the
client. Please click here to learn about Cookies in ASP.NET Core
MVC Applications.
 Session-Based TempData Provider: This provider stores
TempData in a session state. It’s server-side and can handle larger
amounts of data compared to cookies. However, it requires the
application to enable the session state. Please click here to learn
about Sessions in ASP.NET Core MVC Applications.

Note: In ASP.NET Core MVC, if we don’t configure any Provider, it will use
the Cookie TempData Provider by default.

How TempData Internally Works in ASP.NET Core MVC:


Let us understand how the TempData works in ASP.NET Core MVC:
 Setting TempData: When we set a value in TempData, it is stored in
the TempData dictionary. For example, TempData[“Message”] =
“Hello, world!”;
 Storing TempData: At the end of the request, the TempData
dictionary is serialized and stored in the session or cookie by the
Save method.
 Loading TempData: At the beginning of the next request, TempData
is deserialized from the session by the Load method, making it
available for the current request.
 Reading TempData: When you read a value from TempData, it is
marked for deletion but not immediately removed. This ensures it
can still be retained for the next request if needed. For example, var
message = TempData[“Message”] as string;
 Retaining TempData: If you want to keep TempData values for
another request, use the Keep or Peek methods. These methods
ensure that TempData items are not marked for deletion.
Filters in ASP.NET Core MVC

Filters play a crucial role in shaping the behavior of your


applications, providing a powerful mechanism to execute code
before or after certain stages in the request-handling process. They
can be used for cross-cutting concerns such as authorization,
caching, error handling, logging, and more.

The beauty of filters lies in their ability to enhance the modularity


and maintainability of your code.

What are Filters and How They Work in ASP.NET Core?

Filters are classes that implement one or more filter interfaces, such
as IAuthorizationFilter, IResourceFilter, IActionFilter, IExceptionFilter, IRes
ultFilter. Each filter interface defines a pair of methods that are invoked
before and after a specific stage in the request pipeline.

Let’s delve into the core types of filters available in .NET Core:

Authorization filters: run first and determine whether the user is


authorized for the request. They can short-circuit the pipeline if the request
is not authorized. They are executed early in the pipeline before the action
method is invoked. This is the first line of defense for securing your
application.
 Resource filters: run after authorization and can perform
tasks before and after the rest of the pipeline. For example, they
can implement caching or model binding logic.

 Action filters: run before and after an action method is


invoked. They can modify the arguments passed to the action or
the result returned from the action. This is where you can
manipulate the action’s parameters, inspect or modify the result,
and perform additional logic.
Exception filters: run when an unhandled exception occurs
before the response body is written. They can handle the exception
and return a custom response. Here, this filter is applied only for
action methods, resource filters (for any database file access issues),
and model binding. The Exception filter is not application on Result
Filter so for this we need to use the Excepiton middleware concept.

 Result filters: run before and after the execution of action


results. They can modify the result or perform additional actions
such as logging or auditing.

Filters can be applied globally to all actions in the application, or


selectively to specific controllers or actions using attributes. For
example, the [Authorize] attribute is a built-in authorization filter
that requires the user to be authenticated.
The [ResponseCache] attribute is a built-in resource filter that enables
response caching for the action.

Filters can be categorized into two types: built-in filters and


custom filters. Built-in filters are provided by ASP.NET Core and
cover common scenarios such as authorization, caching, exception
handling, and more. Custom filters are created by developers and
can implement any logic that is needed for the application.

The following table summarizes the built-in filters and their use
cases:
Execution Flow of Filters

The order of execution of filters is important because it can affect the


behavior and outcome of the request. For example, if an
authorization filter short-circuits the pipeline, the subsequent filters
will not run. Similarly, if an exception filter handles an exception,
the result filters will not run.

By default, ASP.NET Core executes filters in the following order:

 Global filters, in the order they are registered in


the Startup class

 Controller filters, in the order they are defined on the


controller class

 Action filters, in the order they are defined on the action


method

Within each filter type, the order of execution is determined by


the Order property of the filter attribute or the filter interface. The
lower the value of the Order property, the earlier the filter runs. The
default value of the Order property is zero, which means the filter
runs before any other filter with the same filter type and a higher
order value.

Filters are game-changers for your .NET Core applications. They


offer a plethora of benefits:
 Code Reusability: Write common logic once and apply it
across multiple actions, reducing code duplication and
enhancing maintainability.

 Improved Performance: Cache responses, short-circuit


unnecessary processing, and handle errors effectively, leading to
a smoother, faster application.

 Simplified Development: Focus on core business logic while


leaving cross-cutting concerns like authorization and error
handling to filters.

 Enhanced Security: Implement robust authorization and


input validation checks with filters, adding an extra layer of
protection to your application.

Mastering the art of filters in .NET Core empowers you to build


more modular, maintainable, and secure applications. By
understanding the different types of filters available and their
execution flow, you can harness their full potential to shape the
behavior of your application.
Understanding Action and Non- Action methods, Types of Action Results
in ASP.NET Core.

In ASP.NET, Actions and Non-Actions methods are terms often used in


the context of controllers, which are a fundamental part of the Model-
View-Controller (MVC) architecture used in web applications. These
methods are used to define the behavior of your application when a
specific URL is accessed.

Let’s delve into the differences between Actions and Non-Actions


methods:
1. Action Methods:
Action methods are responsible for handling HTTP requests and
returning HTTP responses. They are the methods that perform specific
actions in response to user requests.
These methods are typically associated with URLs and are used to
execute specific functionality based on the request type (e.g., GET,
POST, PUT, DELETE).

Action methods are public and typically return an ActionResult or one


of its derived types (e.g., ViewResult, JsonResult,
RedirectToActionResult) to determine the response to send back to
the client.

Example of an action method in a controller:


2. Non-Action Methods:

Non-action methods are regular methods in a controller that are not


intended to be used as entry points for handling HTTP requests. They
are auxiliary methods used to factor out common functionality or logic
to be shared among action methods.

Non-action methods are typically marked with


the [NonAction] attribute to explicitly indicate that they should not be
invoked as actions.

These methods cannot be directly accessed via a URL, and they do not
return ActionResults.

Action methods are the main entry points for handling HTTP requests
in ASP.NET MVC controllers. They return ActionResults and are
accessible via specific URLs. Non-action methods, on the other hand,
are regular methods within the controller that are not meant to handle
requests directly and are marked as such to prevent accidental
invocation as actions. They are used for encapsulating logic or
functionality that is shared among multiple action methods.

Now that we understood Action methods and Non- Action methods


lets discuss diferrent types of Action Results that are returned by
action methods.
In ASP.NET Core, Action Results are the types that you return from
action methods in your controller to specify how the HTTP response
should be generated and sent back to the client. Action Results
represent the outcome of an action and dictate what content should be
included in the response. ASP.NET Core provides a variety of built-in
Action Result types to handle different types of responses.

Here are some common Action Results:

1. ViewResult:

Represents an HTML view template that should be rendered and


returned as a response.

Typically used for rendering HTML pages to be displayed in a web


browser.

Example:
2. JsonResult:

Represents JSON data that should be serialized and returned as a


response.

Useful for serving data to client-side JavaScript applications.

Example:

3. RedirectResult:

Redirects the client to a different URL or action method.

Used for performing HTTP redirects.

Example:
4. ContentResult:

Represents plain text, HTML, or any content that doesn’t require view
rendering.

Allows you to specify the content and content type directly.

5. FileResult:

Represents a file download response.

Allows you to send files to the client, such as images, PDFs, or


documents.

Example:

6. StatusCodeResult:

Represents an HTTP status code without a specific content response.


Used for returning HTTP status codes like 404 (Not Found), 500
(Internal Server Error), etc.

Example:

These are some of the commonly used Action Results in ASP.NET Core.
Depending on the desired response and content type, you can choose
the appropriate Action Result type to return from your action methods.

***********************

You might also like