Pete
Pete

Reputation: 58432

Get 404 error page with castle windsor and mvc

I am using Castle Windsor as an IoC with my MVC website project. I have it initialised it by doing the following:

new CastleInitialiser(new WindsorContainer().Install(FromAssembly.InDirectory(new AssemblyFilter(HttpRuntime.BinDirectory, "ProjectName*.dll"))));

And then in my constructor I have the following that registers my controllers:

Container.Register(CastleClasses.FromThisAssembly().IncludeNonPublicTypes().BasedOn<Controller>().LifestylePerWebRequest());

This all works fine but now when I hit a url that doesn't exist, I don't get a 404 error thrown - I get the following error message:

The IControllerFactory 'ProjectName.Website.Castle.IoC.CastleControllerFactory' did not return a controller for the name 'asdasdasasdasd'.

Where asdasdasasdasd is the name of the invalid url

Is there any way to get this so that it throws a 404 exception instead of a castle windsor exception?

Upvotes: 1

Views: 210

Answers (1)

Pete
Pete

Reputation: 58432

In my CastleController class, I am able to throw the 404 like so:

public class WindsorControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null) 
        {
           throw new HttpException(404, "page not found");
        }

        return (IController)container.Resolve(controllerType);
    }
}

Upvotes: 2

Related Questions