ASP.NET Web Pages – Delete Roles

[ This is a 5 part tutorial; previous tutorial, Remove User from Role ]

When deleting roles, users in the roles need to be removed first. This can be done quite simply. Make a new file called DeleteRole.cshtml in the private folder.



 var roles = Roles.GetAllRoles();
 var role  = Request["role"];
 var msg = string.Empty;

   if (IsPost){
   Roles.DeleteRole(role, false);//DELETE ROLE
   msg  = role +" has been deleted";



In this example we first get all the roles, and then get the role we want to delete. We then delete the role; the DeleteRole method takes two arguments (if it is overloaded you can provide a single argument, which is role name) the role name and the role to delete, and if the system should throw an error if users exist in the role (in this case we don’t want to throw an error so we set it to false).


<form method="post">
    <p style="color:#a33f3f">@msg</p>

<legend>Delete Role</legend>

<div class="elements">
     <label for="role">Role:</label>
    <select name="role" id="role">
    @foreach (var uRole in roles){

    <input type="submit" class="submitform"/>


[ Next: Restricting Pages ]