views:

717

answers:

1

Hi folks,

I've got StructureMap working fine on my machine. Everything works great ... until I request a resource that doesn't exist. Instead of a 404, i get a 500 error.

eg. http://localhost:6969/lkfhklsfhskdfksdf

Checking the net, i was told to fix my structuremap controller class. Did that and joy! i now get the -original default 404 yellow screen page-. Ok, that's better than my 500 error page.

BUT, i want it to go to my custom 404 page :( If i call a bad action on a legit controller, i get my custom 404 page.

In my global.asax i have my custom routes, then the default, then finally the 404 route:

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

Here's my structuremap controller code:

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

Any ideas? is there some way i could somehow get the structure map controller factory to bubble back into the global.asax routes list? or have i done something really bad and need to fix up some other stuff.

cheers!

+1  A: 

mmm... seems like it might be an exception thing. like the way MVC is designed to handle the 404 errors via exceptions.

here is my code:

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

.. you may have even had this and changed it. If not, try it and see if there is a difference. I dont suspect there will be.

Maybe just try breaking into that override and seeing what exceptions are being thrown.

(side note: its weird how i keep stumbling on your questions Krome. What r u working on?)

EDIT: I tried the garb request and got the same exception. So i updated my class like you did.

My new class:

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

..this seems to give me back the 404 like it should.. but i never get the custom error pages in development (locally).. i have to wait till i publish before i get those. Are you used to seeing the custom error pages in dev?

cottsak
I'm actually working around 5 sites at the moment :( Re-arching one, 4 are new. :( Also, with your code, i used to have that also. What happens when u goto a url like: http://localhost:12345/sfsfkasfhasklfla (eg. your localhost and port and then a uri which does NOT match any controller. Does your StructureMapControllerFactory throw an exception?
Pure.Krome
My app falls onto the custom error page by default in production.. but not coz i have a '404 route' for it. I think its internal MVC design. Why do you have that 404 route?
cottsak
So i can send peeps to my own custom 404 page
Pure.Krome
in MVC are you not supposed to do that thru the Error view, HandleErrorAtribute or web.config?
cottsak