This video is available to students only

Creating Add Role Endpoint

In this lesson, we're going to create an addRole endpoint

Creating Add Role endpoint#

As we discussed in the last lesson, let's create an endpoint for adding role. For this, we can go to UsersController and create a new endpoint. This will be Authorized endpoint and will be a post request with name, addRole. Let's write the method now; public async task of type actionResult with name add role. Inside, we need a user which is equal to userManager.findByNameAsync and inside, we can pass User.Identity.Name; after this, we can use userManager.AddToRoleAsync and pass user and the role Instructor. Finally, let's return Ok.

API/Controllers/UsersController.cs

Now let's go to the agent file and add this endpoint inside Users. Let's call this addRole and this will be a post request so let's type the url, users/addRole, and pass the empty body.

client/src/actions/agent.ts

client/src/redux/slice/userSlice.ts

Start a new discussion. All notification go to the author.