StructureMap控制器工厂和null控制器实例在MVC [英] StructureMap controller factory and null controller instance in MVC

查看:195
本文介绍了StructureMap控制器工厂和null控制器实例在MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然试图用结构图来计算,我遇到的问题之一是我的控制器工厂类,当一个空的控制器类型传递给它的时候。这只发生在应用程序第一次构建时,之后每个后续构建工作正常。即使我关闭Visual Studio和重新打开项目(我不是在IIS中运行)。这几乎就像有一些缓存。这是控制器类看起来像:

I'm still trying to figure things out with StructureMap and one of the issues i'm running into is my Controller Factory class blowing up when a null controller type is passed to it. This only happens when the application builds for the first time, after which every subsequent build works fine. Even when i shutdown Visual Studio and reopen the project (I'm not running this in IIS). It's almost like there is some sort of caching going on. This is what the controller class looks like:

public class IocControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(Type controllerType)
    {
        try
        {
            return (Controller)ObjectFactory.GetInstance(controllerType);
        }
        catch (StructureMapException)
        {
            System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
            throw;
        }
    }
}

我需要注册每个控制器吗?谢谢。

What could be wrong? Do i need to have every controller registered? Thank you.

推荐答案

我遇到了与ninject建立的控制器工厂相同的问题。

I ran into the same problem with a controller factory built around ninject.

看起来当MVC无法从路由表中解析路由或路由指定一个不存在的控制器时,MVC将为controllertype传递null。我做了两件事来解决这个问题。您可能想检查您的路由表,并添加显示404错误页面的总线路径,如此处所述 .Net MVC Routing Catchall not working

It seems MVC will pass you null for controllertype when it can't resolve a route from the routing table or when a route specifies a none existing controller. I did two things to solve this. You might want to check your route table and add a catchall route that shows a 404 error page like described here .Net MVC Routing Catchall not working

您也可以使用路由调试器检查出错。
http://haacked.com/archive/ 2008/03/13 / url-routing-debugger.aspx

You could also check with the routing debugger what goes wrong. http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

这篇关于StructureMap控制器工厂和null控制器实例在MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆