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

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

问题描述

我仍在尝试使用 StructureMap 解决问题,我遇到的问题之一是我的 Controller Factory 类在传递空控制器类型时爆炸.这只发生在应用程序第一次构建时,之后每个后续构建都可以正常工作.即使我关闭 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 路由 Catchall 不工作

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

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

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