Getting "The resource cannot be found." error when using Ninject

799 views Asked by At

I'm working on an ASP.NET MVC 3.0 application, using Ninject as my dependency injection framework.

So I've inherited my controller from NinjectHttpApplication like so:

public class MvcApplication : NinjectHttpApplication
{
    protected override void OnApplicationStarted()
    {
        base.OnApplicationStarted();
    }

    protected void Application_Start()
    {
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
        AreaRegistration.RegisterAllAreas();
        DependencyResolver.SetResolver(new NinjectDependencyResolver(Kernel));
    }

    protected override Ninject.IKernel CreateKernel()
    {
        return new StandardKernel(new QueriesModule());
    }

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapRoute(
            "Default",
            "{controller}/{action}",
            new { controller = "Home", action = "Index" },
            new string[] { typeof(HomeController).Namespace }
        );
    }
}

But whenever I run the application and try to browse to any of my controllers, I get the error:

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Home/Index

What's causing this and how do I fix it?

1

There are 1 answers

0
Jonathan On

Turns out this was occurring because the NinjectHttpApplication class from which I'm inheriting is calling the OnApplicationStarted() method at startup. So the solution is to remove the Application_Start() method and move all that code into OnApplicationStarted().