JustJohn
JustJohn

Reputation: 1460

Trying to display simple View listing all the rows MVC 6

I get this strange error: MissingMethodException: Cannot create an instance of an interface.

I've made my objects as simple as possible just trying to get a handle on something that works with this Roles management.

The MVC 6 application works for Register and Login for single users. I can even make Roles and assign them on startup. But any attempt to do anything else has me thwarted. I am just trying to display a list of roles.

Controller:

namespace MVC6.Controllers
{

    //[Authorize(Roles = Utilities.Security.AdminRole)]
    public class RolesManagementController : Controller
    {

        // GET: /RolesManagement/

        public ActionResult Index(IServiceProvider serviceProvider)
        {
            var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
            var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();

          return View(RoleManager.Roles.ToList());

        }

Simple Index View:

@model List<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>



@{
    ViewBag.Title = "Roles";
}


<h2>@ViewBag.Title</h2>

<br /><br />

<fieldset>
    <table id="roles" class="display">

        <thead>
            <tr>
                <th width="20%">Role Name</th>
                <th width="20%">Action</th>
            </tr>
        </thead>
        <tbody>
            @if (null != Model)
            {
                foreach (var role in Model)
                {
                    <tr>
                        <td>
                            @role.Name
                        </td>
                        <td>

                        </td>
                    </tr>
                }
            }
        </tbody>
    </table>
</fieldset>

I start the app in debug with a break point inside of the ActionResult, just to see, and it never hits the breakpoint and returns this error:

MissingMethodException: Cannot create an instance of an interface.

I get nothing, blank, nada, when I type "http://localhost:61849/RolesManagement"in the URL.

Upvotes: 1

Views: 218

Answers (1)

Daniel J.G.
Daniel J.G.

Reputation: 34992

Whenever you want to inject a dependency directly into an action method you need to use the [FromServices] attribute. Check the asp docs:

Sometimes you don’t need a service for more than one action within your controller. In this case, it may make sense to inject the service as a parameter to the action method. This is done by marking the parameter with the attribute [FromServices]

So your code would look like:

public ActionResult Index([FromServices]IServiceProvider serviceProvider)
{
    var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
    var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
  return View(RoleManager.Roles.ToList());
}

You might also want to change your controller so those dependencies are provided in the constructor (In which case you don't need the attribute). That would be cleaner IMHO.

Upvotes: 2

Related Questions