.NET 8 💥 - ASP.NET Core Web API Filters

Mohamad Lawand
4 Mar 202415:35

Summary

TLDRThis video tutorial by Muhammad explains how to utilize filters in a .NET API. It covers the flow of HTTP requests through middleware before reaching controllers and actions, and how filters can be added to manage these actions. Muhammad demonstrates creating and implementing both synchronous and asynchronous filters, customizing their execution for specific actions. The tutorial also shows how to combine multiple filters and control their order of execution, offering a comprehensive guide to enhancing web API functionality with filters.

Takeaways

  • 😀 The video discusses the utilization of filters within a .NET API to manage and control actions more effectively.
  • 🔧 Filters in .NET API can be used to add an extra layer of functionality such as logging, auditing, and validation before and after action execution.
  • 🛠 Middleware and filters work together, with middleware processing requests first and filters providing additional control over specific actions.
  • 📝 Middleware is essential for every request, whereas filters can be selectively applied to certain actions or controllers.
  • 📌 Filters can be implemented to run either synchronously or asynchronously, offering flexibility in handling requests.
  • 📝 The script provides a step-by-step guide on creating custom filters by implementing the IActionFilter interface.
  • 🔑 Filters can be attached to actions using attributes, allowing for the specification of action-specific filters.
  • 🔄 The order of filter execution can be controlled, allowing for a structured approach to processing requests.
  • 📊 The video demonstrates how to implement and test filters using a practical example with a Drivers controller.
  • ⚙️ Filters can be used to execute code before and after an action, providing a way to log or modify the request and response as needed.
  • 🔍 The script concludes by showing how to combine synchronous and asynchronous filters for a single action to achieve desired outcomes.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is utilizing filters within a .NET API, explaining how they work, and how to implement them in code.

  • What is the normal flow of a request in a web API?

    -The normal flow of a request in a web API involves the request going to the API, finding the controller and action, executing the action, and then returning a response.

  • What are middlewares in the context of web APIs?

    -Middlewares are layers that a request has to go through before it hits the controller. They can handle tasks such as authentication, authorization, verification, and logging.

  • What is the purpose of filters in a .NET API?

    -Filters in a .NET API provide an extra layer of control over actions. They allow developers to run code before and after an action's execution, giving more control over the request handling process.

  • How can filters be applied in a .NET API?

    -Filters can be applied globally to all actions, or specifically to certain actions or controllers, by attaching them in the code.

  • What is the difference between middleware and filters in terms of execution order?

    -Middlewares always run first for every request, while filters can be specified for individual actions and run before and after the action's execution.

  • Can filters be applied to only specific actions rather than all?

    -Yes, filters can be applied to specific actions or controllers, allowing for more granular control over which requests are processed by the filters.

  • How can multiple filters be ordered in a .NET API?

    -Multiple filters can be ordered by specifying their execution sequence in the code, determining which filter runs before or after another.

  • What is an example of a filter implementation in the video?

    -An example given in the video is a logging filter that outputs messages to the console before and after the action's execution.

  • How can asynchronous filters be implemented in a .NET API?

    -Asynchronous filters can be implemented by using the async/await pattern in the filter's method, allowing the request to move through filters without blocking the execution.

  • What is the benefit of using both synchronous and asynchronous filters?

    -Using both synchronous and asynchronous filters allows for a combination of immediate processing and non-blocking operations, providing flexibility in handling different types of requests.

Outlines

00:00

🔧 Understanding Filters in Web API

In this segment, Muhammad introduces the concept of filters within a web API, explaining their role in processing requests before they reach the controller. He outlines the typical request flow in a web API, emphasizing the importance of middleware that handles tasks like authentication and authorization. Muhammad then transitions into explaining how filters can be used to add an extra layer of control, intercepting requests and responses to perform specific actions like logging or validation. The explanation is geared towards understanding the mechanics of how filters fit into the broader architecture of web API request handling.

05:00

🛠️ Implementing Filters in Code

Muhammad demonstrates how to implement filters in a practical coding scenario. He starts by creating a new directory for filters and then a class named 'MyLogging' that implements the 'IActionFilter' interface. This interface requires implementing two methods: 'OnActionExecuting' and 'OnActionExecuted'. He uses console logging to show the filter's effect before and after action execution. The video then shows how to attach this filter globally to all actions within the 'Program.cs' file, and how to test it using Postman. The segment concludes with a discussion on how filters can be applied to specific actions rather than globally, enhancing the control over request processing.

10:02

📝 Creating and Applying Action-Specific Filters

This part of the video focuses on creating filters that can be applied to specific actions within a controller. Muhammad modifies the 'MyLogging' filter to include a constructor that accepts a color name, making it more versatile. He then shows how to use this filter as an attribute on a specific action within the 'DriversController'. The video illustrates how this filter executes only for the specified action, demonstrating the flexibility of filters in handling requests. The segment also covers the creation of an asynchronous filter, 'MyLoggingAsync', and how to apply it to an action, highlighting the difference between synchronous and asynchronous filter execution.

15:02

🔄 Combining Multiple Filters and Conclusion

Muhammad concludes the tutorial by showing how to combine multiple filters, both synchronous and asynchronous, for a single action. He modifies the 'DriversController' to include both types of filters and demonstrates their execution order. The video emphasizes the ability to control the order of filter execution, which is crucial for managing the request processing pipeline effectively. The segment wraps up with a summary of how filters provide fine-grained control over actions in a web API, allowing developers to add specific functionalities like logging, auditing, or validation. Muhammad invites viewers to ask questions in the comments and encourages support through Patreon or buying him a coffee.

Mindmap

Keywords

💡Filters

Filters in the context of the video refer to a mechanism within web APIs that allow for additional processing or control before and after an action is executed. They are used to manage actions and can be attached to requests to provide more control over how requests are handled. In the script, filters are discussed as an extra layer that can intercept calls before they reach the controller, and they can be applied to specific actions or controllers to perform tasks such as logging, validation, or auditing.

💡Middleware

Middleware is a layer of software that sits between the client and the server in an application. It's responsible for handling different types of tasks such as authentication, authorization, and logging before the request hits the controller. In the video, middleware is depicted as a necessary component that all requests must pass through, unlike filters, which can be selectively applied to specific actions.

💡API

API stands for Application Programming Interface, which is a set of rules and protocols for building and interacting with software applications. In the video, the term is used to describe the web API that the presenter is working with, discussing how to handle requests and responses within this framework.

💡Controller

A controller in web development is a component that handles the incoming requests and invokes the appropriate action based on the request. The script describes how requests flow to the controller, which then executes an action and returns a response. Filters can be applied to controllers to add pre- and post-action execution logic.

💡Action

In the context of the video, an action refers to a method within a controller that is executed in response to a request. Actions are where the business logic of the application is implemented. Filters can be used to execute code before and after these actions are performed.

💡Request

A request in the video script represents an incoming HTTP call to the web API. It is the starting point of the flow that the video discusses, which includes passing through middleware and filters before reaching the controller and action.

💡Response

The response in the video is the outcome or result that the API sends back to the client after processing a request. The script explains how the response is generated after the action has been executed and how filters can modify or log the response before it is sent back.

💡Authentication

Authentication in the video is a process that verifies the identity of a user or system making a request to the API. It is one of the types of middleware that the script mentions as part of the request processing flow.

💡Authorization

Authorization, as discussed in the script, is the process of determining if an authenticated user has the necessary permissions to perform a specific action. It is another middleware function that checks permissions after authentication.

💡Logging

Logging in the context of the video refers to the action of recording events that happen when a request is processed. It is used as an example of a middleware function that can be implemented to keep track of activities within the API.

💡Asynchronous

Asynchronous processing, as mentioned in the script, is a method of executing code where the execution of one part does not need to wait for another part to complete. The video demonstrates how to create asynchronous filters that can be used to perform non-blocking operations within the API request processing flow.

Highlights

Introduction to utilizing filters within a .NET API to understand their functionality and implementation.

Explanation of the request flow in a web API, including the role of controllers and actions.

Discussion on middleware and its role in intercepting requests before they hit the controller.

Introduction of filters as an additional layer for managing actions in a .NET API.

Demonstration of how filters can be attached to requests and their impact on the request-response cycle.

Illustration of middleware and filters working together in a layered approach within an API.

Explanation of how filters can be applied to specific actions, controllers, or globally.

Creation of a custom filter in a .NET project and its implementation as an IActionFilter.

Example of adding a logging filter to demonstrate its usage before and after action execution.

How to attach a filter to every action in a .NET API using the program.cs configuration.

Demonstration of a filter in action using Postman to test API endpoints.

Adjusting filters to execute on a single action rather than all actions within a controller.

Creating an asynchronous filter and its implementation compared to a synchronous one.

Attaching multiple filters to a single action and controlling their execution order.

Practical example of combining synchronous and asynchronous filters in a .NET API.

Final thoughts on the importance of filters in providing more control over API actions and requests.

Closing remarks, invitation for questions, and acknowledgments for the video.

Transcripts

play00:00

Hello friends thank you for watching

play00:01

this video I am Muhammad and today we're

play00:02

going to be discussing how we can

play00:04

actually utilize filters within our. net

play00:06

API we're going to be understanding how

play00:07

do they work how do they fix within

play00:09

middle words and then we're going to be

play00:10

actually seeing how we can Implement

play00:11

them within our code so let's get

play00:13

started so what we're going to be doing

play00:14

right now is we're going to be

play00:15

understanding what's going to happen

play00:16

whenever a request actually comes into

play00:19

our web API so let's say we have a

play00:20

request coming in and within this

play00:22

request we're just going to hit a

play00:23

controller and within this controller

play00:25

we're going to have an action so the

play00:27

request flow will going to be something

play00:28

like this and then once the action

play00:30

finish executing then we're going to get

play00:32

back a response at the end and this flow

play00:34

that we're currently seeing here is

play00:35

basically the normal flow whenever we're

play00:37

handling anything when it comes to a web

play00:39

API we're request we're going to go to

play00:41

our API it's going to find the

play00:42

controller the action the actions going

play00:44

to execute and response but this is not

play00:46

the only way it's going to happen this

play00:48

is a very simplistic view of how stuff

play00:50

happen within nut we're going to have a

play00:51

lot of different layers that's going to

play00:53

request has to go through before it

play00:55

actually even hits the controller ler so

play00:57

we're going to have some like middle

play00:58

Wares available in place and within

play01:00

those middle words the request is going

play01:01

to be basically go through different

play01:03

types of either authentication

play01:05

authorization uh verification Etc and

play01:07

then it will hit the controllers so we

play01:09

can see here that within our net there's

play01:11

a lot of different middle Wares that

play01:12

actually might intercept the call before

play01:14

it actually execute or actually reach

play01:17

the controller and all of this we have

play01:18

discussed previously how actually

play01:20

middleware comes into place and how do

play01:21

they work in conjunction with our

play01:23

controllers on action so on top of that

play01:25

we're going to be discussing today how

play01:27

we can actually add an extra layer in

play01:29

order for us to actually manage our

play01:30

actions and this is going to be

play01:32

something called filters so what we want

play01:33

to do is we want to actually attach a

play01:35

filter to our request and through that

play01:38

the filter will actually then forward

play01:40

the request to response and once the

play01:42

action has finished executing it's going

play01:44

to basically then go to the filter and

play01:46

then instead of the action directly

play01:47

returning the response then through that

play01:49

the response is going to go back and

play01:51

this is what we're going to be doing

play01:52

today we're going to be trying to adding

play01:53

those filters in place and we're going

play01:54

to be understanding how they actually

play01:56

work so what I have here is I have a

play01:58

full example of what's actually going to

play02:00

happen when when the request comes in so

play02:02

this is a HTTP request coming in we can

play02:04

see it's going to hit the middleware So

play02:06

within the middleware we're going to

play02:07

execute some logic once it execute the

play02:09

logic the middleware will forward that

play02:10

request to the second middleware the

play02:11

second middleware will also take this

play02:13

request it will execute another logic so

play02:15

one could be authentication the other

play02:16

one could be authorization the third one

play02:18

could be logging so let's add this here

play02:20

so we can say this is authentication

play02:22

this is authorization this could be

play02:24

logging this could be background jobs I

play02:26

don't know it could be something around

play02:27

those lines ex start executing an in

play02:30

appro and acing background operation and

play02:32

lastly for example here so we're going

play02:34

to have authentication authorization

play02:36

logging and the background jobs and

play02:38

basically we can see here that the

play02:39

request flow is going to go from

play02:40

middleware one once it execute the

play02:41

authentication it will be forwarded to

play02:43

middleware 2 execute the authorization

play02:45

forwarded to middleware 3 execute the

play02:47

author the loging forward it to

play02:49

middleware 4 execute the background

play02:51

shops and then here we want to actually

play02:53

start executing the request when

play02:54

actually it hits the controller so here

play02:56

it's going to be the controller so once

play02:57

it actually hits the controller what do

play02:59

we want to do we want to actually run

play03:01

some code before it actually hits the

play03:02

action which is going to be executing

play03:04

and this is what we're going to be doing

play03:05

we're going to be actually seeing how we

play03:07

can build those layers here filters and

play03:09

these filters here is going to allow us

play03:11

to actually have much more control about

play03:13

those requests so we can have filters

play03:15

here for specific types of auditing we

play03:17

can have filters here for specific types

play03:19

of uh logging mechanism we can have

play03:21

specific types of validation that we

play03:23

want to add so there's L different

play03:25

implementation that we can actually

play03:26

execute on on these different types of

play03:28

filters that we want want to add soil

play03:29

filters here will play a cual role into

play03:31

the actual action itself so all of these

play03:34

different middlewares that we have

play03:35

before these will be executed for every

play03:37

single request coming in there is no way

play03:39

around it it will going to be so let's

play03:41

say we have an an API with 100 end

play03:44

points all of these 100 end points are

play03:46

going to go through in this example

play03:47

through these four different middlewares

play03:49

on the other hand for filters we can

play03:51

specify for every single action its own

play03:53

filters so if we specify a filter that's

play03:55

going to run on only five actions only

play03:56

these five actions will actually have

play03:58

these filters enabl onto them so filters

play04:00

can be on the controllers and they could

play04:01

be as well on the action itself so we're

play04:04

not only limiting it to an an action

play04:06

it's going to be for all request filters

play04:09

for specific actions or controllers and

play04:11

this is going to be the main thing so

play04:13

we're going to be able to have a much

play04:14

more control about what action is going

play04:17

to be executed for which type of request

play04:19

coming in and basically as we once we

play04:22

the request will hit the controller if

play04:24

we have a a filter on the action itself

play04:26

so once that request hits the controller

play04:28

the controller then will forward the

play04:30

request to the filter on that action we

play04:32

execute the filter before the execution

play04:35

of the action then the action is

play04:36

executed and then once that execution

play04:38

has finished execution then we're going

play04:40

to run the code after it uh from the

play04:42

filter side once that's done then we're

play04:44

going to be propagating and popping back

play04:45

the response up into the same layer to

play04:48

the same middleware so it's going to go

play04:49

through the background jobs then it's

play04:51

going to go to the logging to the

play04:52

authorization authentication and then

play04:54

we're going to get back the response so

play04:55

all of this flow that we currently be

play04:57

seeing here is going to be basically

play04:58

building on top of the each other so

play05:00

it's going to go through first second

play05:01

three four middle Wares and once it's

play05:03

going to go through those four middle

play05:04

Wares it's going to go to the action

play05:06

once the action does its work the filter

play05:08

is going to be on the executing there as

play05:10

well before and after and then we're

play05:11

going to be returning back the response

play05:13

so we can think this as a layered cake

play05:15

and this within this layer cake the

play05:17

filter is going to be sitting all the

play05:18

way on top and the uh middle Wares are

play05:20

basically going to be supporting this so

play05:22

as we can see here that this is also

play05:24

going to run a in a very structured

play05:26

approach so the middlewares always have

play05:28

to run first and then we're going to

play05:30

have to run the filters when it comes to

play05:31

filters we can have for example within a

play05:34

single action we can have different

play05:36

filters so we can have two three four

play05:38

filters and we can actually specify the

play05:40

order so so in this example here we have

play05:42

added another filter so we have filter

play05:44

one here run before then we're going to

play05:45

go to filter two then once it have run

play05:48

then we're going to go to the execution

play05:49

then it's going to go to filter two then

play05:51

to filter one and the nice thing about

play05:52

if we're going to add multiple filters

play05:54

filters together we can actually add

play05:56

ordering to them so we can say this is

play05:57

going to run before this or this going

play05:59

to run before for this and we're going

play06:00

to be seeing how we can actually

play06:01

Implement something similar to this so

play06:03

now that how we have understood how

play06:04

filters and middleware works together

play06:06

let's jump into our code and see how we

play06:08

can Implement something like that so in

play06:09

this example here we have the same

play06:11

project that we have always been working

play06:12

on where we have a controller we have a

play06:14

driver's controller and we have an

play06:16

achievement controller we're basically

play06:17

doing a different crubs operation on our

play06:19

controllers here so we can add a driver

play06:21

remove a driver delete a driver and add

play06:23

all of them we're using a SQL light

play06:25

database and if we go to the program.cs

play06:27

we can see here that we barely have any

play06:29

middleware so we have basically the htps

play06:31

redirection middleware we have the

play06:33

authorization middleware and the map

play06:34

controllers middleware so these are the

play06:36

middlewares that we currently have don't

play06:37

really have any other middlewares

play06:39

running into place so what we want to do

play06:40

right now is we want to create our own

play06:42

filter and then once we create our own

play06:44

filter we're going to attach it to one

play06:45

of these controllers actions and then

play06:47

from there we're going to be seeing how

play06:48

that will work so what I want to do here

play06:50

in the root directory I'm going to

play06:51

create a new directory and I'm I'm going

play06:53

to call this directory filters I can

play06:55

call it whatever I want perfect so now

play06:57

that I have created filters I'm going to

play06:58

add a new class and this class we're

play07:01

going to name it my logging okay great

play07:03

so now that my class is up and running

play07:04

what I want to do is I want to inherit

play07:06

or basically I want to implement an

play07:07

interface called I action filter and as

play07:10

you can see here it's asking me directly

play07:11

to implement one of the methods so I'm

play07:13

going to implement this meth method and

play07:15

click okay and it's going to ask me for

play07:17

Implement another one okay perfect so

play07:18

now that I have implemented both methods

play07:20

we can see here that difficult

play07:21

implementation that I need to do which

play07:23

is going to be on action executing and

play07:26

on action executed so the first thing

play07:27

that I want to do to make it as simple

play07:29

as possible is directly output stuff

play07:31

onto the console so console do right

play07:34

line we're going to say this filter

play07:36

executed before I'm going to copy this

play07:39

put it here I'm going to say here filter

play07:40

executed after so now that I have done

play07:42

this I now created my first filter and

play07:45

then basically I attached the action to

play07:46

it once I have done this I want to

play07:48

basically go to my program.cs and what I

play07:50

want to do is I want to attach it now

play07:51

just so we can see it works to every

play07:53

single action that currently exists so

play07:55

in order for me to do that on the

play07:57

Builder do service of add controller I

play07:58

want to configure it to actually utilize

play08:00

this filter so I'm going to put options

play08:02

options do filters do add and I'm going

play08:05

to say new my loging pretty

play08:07

straightforward so now if I want to run

play08:09

this so now that my application is

play08:10

running let's go to postman and within

play08:12

Postman I'm going to get all the drivers

play08:14

and now we should be able to see that I

play08:16

got one driver that currently exist and

play08:18

if I go back to the log we can see here

play08:20

I have filter executed before and I have

play08:22

filter executed after and we can see

play08:24

inside this is my command to the

play08:25

database to execute the to extract the

play08:27

driver's information so we can see here

play08:29

now that directly my filter is actually

play08:31

running whenever I execute any command

play08:33

so if I try to run this again multiple

play08:35

times you should be able to see that

play08:36

this is directly after the execution

play08:38

filter executed before executing again

play08:41

then executing after similar executed

play08:43

before then executed after so we can see

play08:45

that this is working as it should be

play08:47

let's try different endpoint so let's

play08:48

say I want to get driver ID specify the

play08:50

driver send we can see I'm getting a

play08:52

single driver here again for this as

play08:54

well I'm getting executed before and

play08:56

we're getting here as well filter

play08:58

executed after so this is all fine and

play09:00

uh good but filters as we said we have

play09:03

the power to actually specify them into

play09:05

a single action rather than executing

play09:06

them on all actions and controllers so

play09:08

let's see how we can actually only

play09:09

execute this on a single action so I'm

play09:11

going to command this out and I'm going

play09:12

to go to my loging here and I'm going to

play09:14

update this to actually be able to

play09:16

specify it and to make it as an

play09:18

attribute available so the first thing

play09:19

that I want to do is I want to put here

play09:21

attribute and now once I put an

play09:22

attribute here it means that I'm

play09:24

actually able to make it as an attribute

play09:26

and I want to update this in order for

play09:27

me to take advantage of that so first of

play09:30

all what I want to do is I'm going to

play09:31

put private read only string and I'm

play09:33

going to specify something called a

play09:35

Coler name and I'm going to initialize

play09:36

this through the Constructor okay

play09:38

perfect and now once I have done that

play09:40

what I want to do here is I want to

play09:42

actually specify the Coler name so I'm

play09:44

going to put here string interpolation

play09:45

and I'm going to specify here that I'm

play09:47

going to use the Coler name similarly

play09:49

I'm going to do the same thing here and

play09:51

now that I've done this I'm going to go

play09:52

to my driers controller and for the get

play09:55

old drivers which is the one that we

play09:56

have used before can add a filter here

play09:58

and this filter is going to be my

play10:00

logging and for this we're going to give

play10:02

it a name or basically the Coler name

play10:04

that we currently specified here and for

play10:06

this I'm just going to call it all

play10:08

drivers so let's run this and see how

play10:10

actually going to execute so now this is

play10:11

running I'm going to go back to postman

play10:13

and if I run this right now we can see

play10:15

here this is with an ID I should not be

play10:17

able to see that so now this is running

play10:19

I got my response back if we go back to

play10:21

postman we can see here that I did not

play10:23

get that that logging inside my console

play10:25

so now if I remove this and I'm now

play10:27

doing get all driers I click on send we

play10:30

get all drivers here and we can see here

play10:32

that I got the filter only executed on

play10:34

this so we can see filter executed

play10:35

before all drivers and filter executed

play10:38

after all drivers so we can see here

play10:40

that this filter only executed on one

play10:42

single action rather than having it

play10:44

executed on all different actions so if

play10:45

I try to put this back here the driver

play10:48

ID that I have and I try to put it here

play10:49

and click on send we can see I got back

play10:51

the response but here we can see the

play10:53

filter did not execute okay perfect so

play10:55

now that I have was able to create the

play10:57

filter only on single action and I was

play10:59

able to actually pass information to my

play11:01

filter for me to be able to utilize it

play11:03

let's see how we can actually have a

play11:04

filter which is going to be async so

play11:06

here if I go to my logging filter that I

play11:08

created let me stop my application we

play11:10

can see here that everything is running

play11:11

in a synchronous matter if I want to

play11:13

execute an async one so I'm going to

play11:15

create a new class I'm going to call it

play11:16

my logging async and then here what I

play11:18

want to do is I'm going to add it as an

play11:20

attribute as I did before but the one

play11:22

difference that I want to do here is I'm

play11:23

going to put I a action filter and once

play11:26

I have added this it's going to ask me

play11:27

to actually implement the member and we

play11:29

can see here instead of having two uh

play11:31

members which is on on action executing

play11:33

and on action executed I have one single

play11:36

member and let me just put this so you

play11:37

can see it on a single line but then the

play11:39

asynchronous one I get only one method

play11:41

and this method is contain a context and

play11:44

contain an next so here what I need to

play11:45

do is in order for me to make this

play11:47

functional I need to put away next I

play11:49

need to make this as an async and now

play11:51

basically what I'm doing here is if we

play11:53

go back to my diagram I'm actually

play11:55

allowing this to run in a similar manner

play11:57

to this where I'm having the next object

play11:58

object in order for me to move it from

play12:00

one to another I'm utilizing this

play12:02

asynchronous approach in order for me to

play12:03

allow the movement of the request from

play12:06

one filter to another so if I do here

play12:08

like this console do right line I'm

play12:10

going to put this before the before the

play12:13

request goes execute and we're going to

play12:16

put after the request execute and I'm

play12:18

going to do the same thing as I done

play12:20

here I'm just going to copy this so I'm

play12:21

actually able to have a reference to

play12:23

Who's Calling it and I'm going to attach

play12:25

this here and I'm going to put here it's

play12:26

going to be Caller Name make this as an

play12:29

synchron as string interpolation

play12:31

similarly here perfect so now that I

play12:33

have done this let's see how we can

play12:34

actually utilize this my loging async

play12:36

I'm going to copy this action I'm going

play12:38

to copy the name of my class go to my

play12:40

controllers and I'm going to add this

play12:42

only when getting a single driver so I'm

play12:44

going to go here to set get a single

play12:45

driver I'm going to add this here and

play12:47

then I'm going to specify my color name

play12:49

and I'm going to say this color name

play12:51

it's going to be get single driver so

play12:53

now if I execute this we can see it's

play12:55

running if I go back to postman now if I

play12:57

get this run this this we're going to

play12:59

get Lewis perfect if I go back to Rider

play13:02

we can see here that before the request

play13:04

execute get single driver and then after

play13:06

the request execute G single driver this

play13:08

is an async filter here we can see the

play13:10

other one did not come into place if I

play13:12

remove the ID here in order for me to be

play13:15

able to get all the drivers and click on

play13:17

send we'll be able to see I get my

play13:18

synchronous approach which is going to

play13:20

be filter executed before all drivers

play13:22

and filter executed after all drivers

play13:23

compared to the one before that which is

play13:25

after the request executed so which is

play13:27

going to be async or not let just make

play13:29

the text a bit more clearer so I'm going

play13:30

to put here sync so we know the

play13:32

difference and here sync as well here

play13:34

I'm going to put a sync and I'm going to

play13:36

put here a sync okay perfect so let us

play13:38

run this again get all drivers so this

play13:40

should be the synchronous request we can

play13:42

see I have my synchronous request coming

play13:44

here and my synchronous request coming

play13:46

here okay perfect if I add the driver ID

play13:48

click on send now if I go down I can see

play13:50

my a synchronous one is running and I

play13:52

have my a synchron this one is running

play13:53

as well so that's great so one of the

play13:55

last items that we want to cover today

play13:57

is we're going to be seeing how we can

play13:59

actually attach two of them together so

play14:01

what I'm going to do is I'm going to

play14:02

take the asynchronous one and I'm going

play14:04

to add it to get all drivers so all I'm

play14:06

going to do here is after all my logging

play14:07

all drivers I'm just going to update

play14:09

this here and I'm going to run my code

play14:11

again I'm going to go back to postman

play14:13

and forget all drivers let's see what's

play14:15

going to happen now we got the response

play14:17

pack if I go back here we can see here

play14:19

that first the synchronous one has run

play14:21

and then the asynchronous one has run

play14:22

afterward then the asynchronous finished

play14:24

executing and then the synchronous one

play14:25

here so we can see within this we are

play14:27

actually able to attach multiple filters

play14:29

together in order for us to get the

play14:31

response that we want and to get the

play14:32

action that we want and if I change the

play14:34

order of this so if I put first of my a

play14:36

synchronous and then my synchronous I'm

play14:38

going to stop this and I'm going to run

play14:40

this again go back to postman execute it

play14:43

cut back the response and now we can see

play14:44

here that the first one has happened

play14:46

which is the asynchronous one and then

play14:47

the synchronous one has has executed

play14:49

synchronous finished executing and the

play14:51

async has executed okay perfect So

play14:54

within this we were able to see how

play14:55

filters can actually work in conjunction

play14:58

with middle in order for us to have much

play15:00

more control about our actions and

play15:02

basically we're able to specify specific

play15:04

actions to run on specific request

play15:06

rather than having it run on all of the

play15:08

single rather than having it running on

play15:10

all of the incoming requests filters

play15:11

allow us to provide much more control on

play15:14

what types of functionalities we want to

play15:16

add to controllers to actions we can

play15:18

specify the orders we can have actually

play15:20

synchronous and asynchronous filters

play15:22

available for us with that said I hope

play15:24

this video was helpful if you have any

play15:25

questions please make sure you put them

play15:27

on the comments down below if you have

play15:28

um if you'd like to support me please

play15:30

consider supporting me on patreon or

play15:31

buying me a coffee with that said thank

play15:32

you very much for watching and have a

play15:34

great day

Rate This

5.0 / 5 (0 votes)

Ähnliche Tags
Web APIFiltersMiddlewareAction ControlRequest FlowCode ImplementationAuthenticationAuthorizationLoggingAsync FiltersAttribute Filters
Benötigen Sie eine Zusammenfassung auf Englisch?