当请求的url / controller不存在时抛出404

时间:2012-02-02 20:24:56

标签: asp.net-mvc castle-windsor

我已经修改了我的WindsorControllerFactory,当请求的控制器不存在时抛出HttpException 404

但是我不确定这是否是一个很好的方法,任何人都知道这有什么问题吗?

public class WindsorControllerFactory : DefaultControllerFactory
{
    readonly IWindsorContainer container;

    public WindsorControllerFactory(IWindsorContainer container)
    {
        this.container = container;
        var controllerTypes =
            from t in Assembly.GetExecutingAssembly().GetTypes()
            where typeof(IController).IsAssignableFrom(t)
            select t;
        foreach (var t in controllerTypes)
            container.Register(Component.For(t).LifeStyle.Transient);
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        //if (controllerType == null) return null;
        if (controllerType == null) throw new HttpException(404, "page not found");
        return (IController)container.Resolve(controllerType);
    }
}

1 个答案:

答案 0 :(得分:1)

一如往常一个开始这种事情的好地方是在温莎维基看到http://stw.castleproject.org/Windsor.Windsor-tutorial-part-two-plugging-Windsor-in.ashx