MVC 中的 StructureMap 控制器工厂和空控制器实例

发布于 2024-07-21 06:50:03 字数 666 浏览 6 评论 0 原文

我仍在尝试使用 StructureMap 来解决问题,我遇到的问题之一是当将空控制器类型传递给它时,我的控制器工厂类会崩溃。 仅当应用程序第一次构建时才会发生这种情况,此后每个后续构建都可以正常工作。 即使我关闭 Visual Studio 并重新打开项目(我没有在 IIS 中运行它)。 这几乎就像正在进行某种缓存。 这就是控制器类的样子:

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;
        }
    }
}

可能出了什么问题? 我需要注册每个控制器吗? 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一抹微笑 2024-07-28 06:50:03

大多数浏览器在加载网站时都会寻找 favicon.ico,并且此行为可能涉及一些缓存,这可能解释了您提到的奇怪的“仅在第一次构建时失败”的情况。

就我而言,这导致了控制器工厂中控制器类型为空的问题。

在 global.asax 中添加 routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); 使得错误消失,请求应该落入文件系统,而无需 MVC 在代码中查找 favico.ico 控制器。

这是 Gunnar Peipman 发布了关于此内容

我通过在自定义控制器工厂类中重写 GetControllerType(string controllerName) 并检查每个请求的controllerName 值发现了这一点。

Most browser are looking for a favicon.ico when you load a site, and there is probably some caching involved with this behavior, this might explain the odd "Only fail on the first build" thing you mentionned.

In my case this was causing the problem of the null controller type in the controller factory.

Adding a routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); in global.asax makes the error go away, the request should fall through to the filesystem without MVC looking for a favico.ico controller in your code.

Here is a link to Gunnar Peipman post about this

I found out by overriding GetControllerType(string controllerName) in my custom controller factory class and checking what the controllerName value was for each request.

诗笺 2024-07-28 06:50:03

我在围绕 ninject 构建的控制器工厂中遇到了同样的问题。

当 MVC 无法解析路由表中的路由或路由指定不存在的控制器时,MVC 似乎会为控制器类型传递 null。 我做了两件事来解决这个问题。 您可能需要检查路由表并添加一个显示 404 错误页面的包罗万象的路由,如下所述 .Net MVC Routing Catchall 不起作用

您还可以使用路由调试器检查出了什么问题。
http://haacked.com/archive/2008/03/ 13/url-routing-debugger.aspx

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

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

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

陌路黄昏 2024-07-28 06:50:03

我也遇到了类似的问题。 我相信这是针对不存在的图像、CSS 文件等的 HTTP 请求。

我们知道 MVC 路由首先查看所请求的文件是否物理存在。 如果没有,则根据配置的路由测试 URL。 我认为对物理上不存在的图像的请求被传递到路由引擎,并且不匹配任何路由,因此使用了 NULL。

因此,要修复它,请使用 FireBug 或其他东西来监视并修复损坏的 HTTP 请求。 在开发过程中,我使用这样的路线来暂时绕过这些问题(我的所有资源文件夹都以下划线开头,如 _Images、_Styles 等):

routes.IgnoreRoute("_*");  // TODO: Remove before launch

希望这会有所帮助!

I was having the similar problem. I believe it was HTTP requests for nonexistent images, CSS files, etc.

We know the MVC routing first looks to see if the requested file physically exists. If it doesn't then the URL gets tested against the configured Routes. I think the request for an image that didn't physically exist was passed to the Routing engine, and didn't match any routes, so NULL was used.

So to fix it, use FireBug or something to watch for, and fix, broken HTTP requests. During development, I used a route like this to temporarily bypass these issues (all of my resource folders start with an underscore like _Images, _Styles, etc):

routes.IgnoreRoute("_*");  // TODO: Remove before launch

Hope this helps!

清晰传感 2024-07-28 06:50:03

我认为您需要做的事情与默认 MVC 控制器工厂在 GetControllerInstance 方法上所做的事情完全相同。 如果您在 http://aspnetwebstack.codeplex.com/ 查看 DefaultControllerFactory 的 Microsoft 源代码,您将看到当controllerType为null时,DefaultControllerFactory会抛出404异常。 以下是我们根据此信息执行此操作的方法:

 public class StructureMapControllerFactory : DefaultControllerFactory
 {
      protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
      {
           if (controllerType == null)
                return base.GetControllerInstance(requestContext, controllerType);
           var controller = ObjectFactory.GetInstance(controllerType);
           return (IController)controller;
      }
 }

基本上,这将确保当用户输入无效路由时,应用程序将其作为 404 错误进行处理。

What I think you need to do is exactly the same thing that the default MVC controller factory does on the GetControllerInstance method. If you look at the Microsoft source code for DefaultControllerFactory at http://aspnetwebstack.codeplex.com/ you will see that the DefaultControllerFactory throws a 404 Exception when controllerType is null. Here is how we do it based on this information:

 public class StructureMapControllerFactory : DefaultControllerFactory
 {
      protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
      {
           if (controllerType == null)
                return base.GetControllerInstance(requestContext, controllerType);
           var controller = ObjectFactory.GetInstance(controllerType);
           return (IController)controller;
      }
 }

Basically this will ensure that, when user enters an invalid route the application handles it as a 404 error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文