Lecture thumbnail 0:00 / 0:00 The concept of map groups or route groups is a concept of grouping your endpoints based on a

particular route prefix. That means instead of you write multiple endpoints that have the common

prefix independently, you can group them together so that you will be having the chance of separating

the same into a separate file instead of writing too much code in the program.

First, you will be creating the map group by calling this map group method and you will apply the route prefix, for example, products.

And then you can create multiple endpoints based on that map group.

The map group method returns an object of route group builder class and that object can be received into this variable based on which you can create one or more endpoints.

So if you apply any kind of endpoint filter or authorization policy upon this map group,

it gets applied for all these endpoints at a time.

Much like controller has action methods, map groups have multiple endpoints.

Let me show that. First of all, let us try to create a map group over here. Hey, application builder, create a map group for me. The route prefix is the product.

So it creates and returns an object of route group builder class, which can represent a map group, and we are receiving the same instance into this reference variable.

Now onwards, you will be writing map group dot map get.

The same code works.

In the same way for others too.

Map group dot map get.

Same way one more.

Now you can see that the route prefix that was applied over here at the very beginning will get commonly applied for all the endpoints of the particular group.

So you need not rewrite the same prefix every time.

You can simply assume that by default it has products as route and you can continue the remaining part if any.

And even for this also, by default it contains products slash.

So single slash is enough.

You can assume that it will be equivalent to products slash.

Because this is the prefix.

And in the same way for the map post also the route prefix products gets applied.

Okay, let’s test the same.

So let’s make a request to products.

And it is get request.

And we are getting all the products as json format.

As expected.

But if you make a request to products slash particular id for example one.

Then it returns the corresponding product object.

That is the single object.

But if you make a post request.

For the same url products.

In all these cases the route prefix products is common.

And we are adding the products information in json format as a part of the request body.

Now make a request.

It says product has been added into the collection.

And you can get back the same when you say.

A get request for the same url products.

Yes, we can see the same product has been added over here.

That’s fine.

So it is working as it before.

So the main benefit of using the map group is.

It applies the route prefix commonly for all the endpoints.

And in future you can apply the filters.

Or authorization policies commonly for the entire map group.

And also you have another chance of separating the code into another file.

Instead of filling the complete code in the program.cs file.

That is you have to write an extension method for this.

Route group builder class.

So let’s create a separate folder for the same.

Right click on the project add a new folder.

Folder name can be anything for example.

Route groups.

Now right click on this route groups.

Add new item.

Selecting a class.

And file name is.

Products map group.

You can say it is route group or map group.

Both means the same.

OK add button.

Now make this class as a static class.

Because we want to create an extension method.

So the class must be a static class.

Now create an extension method.

It can be for example products API.

Or else anything.

You can give it any other name.

But this must be added.

To the particular class called.

Route group builder.

Oops remove the above namespace.

And it returns the same type of object.

So because of this keyword.

This is an extension method that gets injected into the existing class.

That is route group builder.

And at the end of the method you will return the same.

Return the group.

Now you can invoke this products API method.

As an extension method in this program.cs file.

See in the program.cs file now.

You can call.

Products API method.

By the way we have to import that user defined namespace called route groups.

Which we have created over here.

OK.

So now we are invoking our custom code.

That is extension method.

Now what are the endpoints that we have created so far.

Starting from map get up to map post.

Cut this entire piece of the code.

And shift the same into our custom class.

I mean into this extension method.

Pasting the same.

Now instead of you use the map group dot map get.

We can use a group dot.

Here the parameter called group represents the current working instance of the map group.

I mean current instance of the route group builder.

So you can add the endpoints by using any map method.

Such as map get or map post.

Into this group object.

Group dot map get.

Group dot map post.

Like that.

But we need the products information right.

So switch back to the program.cs file.

Cut this products collection from this file.

And paste the same as a part of the extension method.

And optionally make it as a static field in the static class.

So it gets created only one time.

Okay.

So in this static class we have the collection.

And we have an extension method.

That adds endpoints into this route group builder.

And we are calling this extension method over in this program.cs file here.

And it is optional to receive that object here.

Of course you can also.

Now test it again.

The same route prefix work as it before.

For example if you make a request to products.

Yes we are getting the same information.

Of course every time the collection gets reset.

If you want to persist the previous session information.

We have to use the databases.

So you can create the complete architecture.

Such as a repositories services.

Unit tests as we have done earlier.

And you can invoke the service methods as a part of the endpoints.

Okay that’s up to you.

So the API is working fine here.

As it before.

So the benefits for us is.

It will be easy to separate the code into a separate file.

Instead of writing the complete code in the program.cs file.

And make it lengthy.

For larger applications where you have too many number of tables.

This is very useful to organize your code.

You will apply the route prefix.

And invoke the extension method.

That’s it.

And optionally we can also create the endpoints for the.

Put as well as delete requests.

So we have map get.

Another map get for id.

And post request.

Now we are writing the continuation.

Map put.

So if you receive the request at.

Products URL.

And supply the id as argument.

Here the prefix products is applied automatically.

So it will be assumed as products slash id.

Much like this.

And within this lambda expression.

You can receive the product object from the request body.

And we are just copying this piece of the code into this put.

So first we are trying to get.

If matching id is found here.

We are trying to fetch the corresponding product object based on the given id.

And if the product is null.

We will return the bad request result.

But in case if it is not.

We have to update the product details.

The product dot product name.

Oops the parameter name and variable name is same.

Let’s name it as product from collection.

So we are reading the information from the parameter.

And assigning the same into the object from the collection.

Okay we can update the remaining properties that you want.

After updation.

We can send.

The success response.

And by the way we can receive the id as parameter.

So this id parameter value come from the route parameter.

And optionally if you want to specify exclusively.

You can mention the same from body.

You can apply these attributes such as from body from query etc.

As we have learned in model binding.

Okay add a breakpoint here.

And then run.

Now we are going to make a put request.

Select put request over here.

And the url is product slash the id.

For example 1.

I would like to update the particular product where product id equal to 1.

And then coming to the request body.

We have to provide the corresponding details that you want to update.

So id doesn’t change since it is the primary key.

At least theoretically.

But product name can be changed.

It can be anything that you want.

Now click send.

Okay it says product updated.

Now.

If you make a request to products.

As you can see the first product has been changed.

The product name has been changed.

And this is what we want.

So we have successfully created put request for the products table or products collection.

Okay we have added the breakpoint in the wrong place.

That’s why it doesn’t hit.

It’s okay.

We could have added it here.

Now let us try to create the endpoint for the delete request.

Now copy this existing piece of the code of put.

Up to here.

And paste the same below.

Okay this is the copied code.

We are receiving the delete request.

With the same URL products slash id.

And if the product object is not found.

We will return bad request.

But in case if it is found.

We have to remove the same from the collection.

Hey collection.

I would like to remove.

A particular object.

Yes product from collection.

This is the object I would like to remove.

After that we can send a success response.

Product deleted.

Okay add a breakpoint.

And it is map delete.

Okay run this application now.

For example, I would like to delete the particular product where id equal to two.

So the URL is products slash one.

Type of the request is delete.

And the request body doesn’t contain anything.

Okay send a request.

Now we are getting error.

Bad HTTP request exception.

It is because we are trying to receive the model object from the request body.

But the put request doesn’t contain the request body.

It is because in the delete request.

We are trying to receive the product object from request body.

The delete request doesn’t contain the request body.

So remove that parameter.

It contains only one parameter called id.

And that too it is coming from the route parameter.

Run this again.

So we are making a delete request with the same URL.

No request body.

So click on none.

And send the request.

Yes we have received the request.

And this is the id from route parameter.

We are fetching the information from the products collection.

And it is not null.

We have an existing product object.

Where id equal to one.

And we will remove that object from the collection.

And send the success response to the client.

Product deleted.

HTTP 200.

That’s fine.

We have created get, post, put, delete all the CRUD operations through minimal API.

Alternatively, you can also create the complete hierarchy and architecture.

Starting from db context, repository layer, service layer.

And you can invoke those service methods as a part of your endpoints.

So it is up to you.

At your practice time you can try the same.