【问题标题】:Having problems with ASP.NET MVC and StructureMapASP.NET MVC 和 StructureMap 存在问题
【发布时间】:2010-10-30 05:43:39
【问题描述】:

我的机器上的 StructureMap 工作正常。一切都很好......直到我请求一个不存在的资源。我得到一个 500 错误,而不是 404。

例如。 http://localhost:6969/lkfhklsfhskdfksdf

上网查了一下,有人告诉我fix my structuremap controller class。做到了,很高兴!我现在得到 -原始默认 404 黄屏页面-。好的,这比我的 500 错误页面要好。

但是,我希望它转到我的自定义 404 页面 :( 如果我在合法控制器上调用错误操作,我会得到我的自定义 404 页面。

在我的 global.asax 中,我有自定义路由,然后是默认路由,最后是 404 路由:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Post", action = "Index", id = "" } // Parameter defaults
    );

// Invalid/Unknown route.
routes.MapRoute(
    "404-ResourceNotFound",
    "{*url}",
    new { controller = "StaticContent", action = "ResourceNotFound" }
    );

这是我的结构图控制器代码:

public class StructureMapControllerFactory: DefaultControllerFactory {
    protected override IController GetControllerInstance(Type controllerType) {
        if (controllerType != null) {
            return ObjectFactory.GetInstance(controllerType) as Controller;
        return base.GetControllerInstance(controllerType);
    }
}

有什么想法吗?有什么方法可以让结构映射控制器工厂重新回到global.asax 路由列表中?还是我做了一些非常糟糕的事情,需要修复一些其他的东西。

干杯!

【问题讨论】:

    标签: asp.net-mvc structuremap http-status-code-404


    【解决方案1】:

    嗯...似乎这可能是一个例外。就像 MVC 旨在通过异常处理 404 错误的方式一样。

    这是我的代码:

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

    .. 你甚至可能已经拥有并改变了它。如果没有,请尝试一下,看看是否有区别。我不怀疑会有。

    也许只是尝试闯入该覆盖并查看引发了哪些异常。

    (旁注:奇怪的是我总是在你的问题上绊倒克罗姆。你在做什么?)

    编辑:我尝试了装束请求并得到了同样的异常。所以我像你一样更新了我的课程。

    我的新课:

    public class StructureMapControllerFactory : DefaultControllerFactory
    {
        protected override IController GetControllerInstance(Type controllerType)
        {
            if (controllerType == null)
                return base.GetControllerInstance(controllerType);
    
            IController result = null;
            try
            {
                result = ObjectFactory.GetInstance(controllerType) as Controller;
            }
            catch (StructureMapException)
            {
                System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
                throw;
            }
    
            return result;
        }
    }
    

    ..这似乎像它应该的那样给了我 404.. 但我从来没有在开发中(本地)得到自定义错误页面.. 我必须等到我发布之前我得到那些。您是否习惯于在 dev 中看到自定义错误页面?

    【讨论】:

    • 我现在实际上在 5 个站点周围工作 :( 重新构建一个,4 个是新的。:( 另外,使用你的代码,我曾经也有这个。当你转到像这样的 url:localhost:12345/sfsfkasfhasklfla(例如,你的本地主机和端口,然后是一个不匹配任何控制器的 uri。你的 StructureMapControllerFactory 是否抛出异常?
    • 我的应用程序在生产中默认进入自定义错误页面。但不是因为我有一个“404 路由”。我认为它的内部 MVC 设计。为什么你有那个 404 路由?
    • 所以我可以将窥视发送到我自己的自定义 404 页面
    • 在 MVC 中,您不应该通过错误视图、HandleErrorAtribute 或 web.config 执行此操作吗?
    • 别忘了我的updated 404 answer
    猜你喜欢
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2022-09-30
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多