Issue in HandleUnknownAction method for custom Controller

Last post 03-18-2008 5:47 PM by tgmdbm. 8 replies.

Sort Posts:

  • Issue in HandleUnknownAction method for custom Controller

    03-06-2008, 1:19 AM
    • Loading...
    • IMarshal
    • Joined on 07-30-2007, 3:56 PM
    • Posts 6

    I've just completed watching the Scott Hanselman screencasts of the new MVC release (congrads on the fast update!) and have encountered an issue with the HandleUnknownAction.  Here's how you can reproduce the issue:

     1) Modify Global.asax.cs adding the following line

    ControllerBuilder.Current.SetControllerFactory(typeof(Qubit.Controllers.InterceptingControllerFactory));

     2) Create a new file (I followed Scott's example) titled 'InterceptingControllerFactory.cs' in the Controllers folder

    public class InterceptingControllerFactory : DefaultControllerFactory
        {
            protected override IController GetControllerInstance(Type controllerType)
            {
                IController controller = base.GetControllerInstance(controllerType);
                return new InterceptionController(controller);
            }
        }

    3) Create a new file (again following Scott's example) titled 'InterceptionController.cs' in the Controllers folder
     public class InterceptionController : Controller
        {
            private IController _controller;

            public InterceptionController(IController interceptedController)
            {
                this._controller = interceptedController;
            }

            protected override void HandleUnknownAction(string actionName)
            {

                // Shouldn't this only execute for an unknown action?

                // It is executed for even the default url of http://localhost:49327/
                //throw new InvalidOperationException(String.Format("Sorry but we don't allow '{0}' actions.", actionName));
                
                this._controller.Execute(this.ControllerContext);
            }
        }

     

    My expectation is for the HandleUnknownAction method to only execute on a Missing Action method when in fact it executes for URLs such as the default empty URL which is still handled by the routing engine.  Is my assumption incorrect or is this a bug?

     

    Thanks! 

  • Re: Issue in HandleUnknownAction method for custom Controller

    03-06-2008, 8:01 AM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 637
    • ASPInsiders

    Of course,

    You're returning an InterceptionController which doesn't have ANY actions. So ALL actions will be Unknown.

    What you probably want to do is create a MyControllerBase class which overrides the HandleUnknownAction method. Then make all your controllers inherit from that and you're good to go.

  • Re: Issue in HandleUnknownAction method for custom Controller

    03-06-2008, 4:23 PM
    • Loading...
    • IMarshal
    • Joined on 07-30-2007, 3:56 PM
    • Posts 6

     Thanks for the reply.  If you watch the screencast I'm referring to you'll see that Scott derives the HomeController from the standard Controller class as opposed to the InterceptionController.  Secondly, his code in the interception controller used a string comparison to throw an exception if you requested a specific action.  While it was for example/screencast purposes, that would defeat the intention of the HandleUnknownAction method - call me if the user requested an Action which doesn't exist.

    You can find the screencast http://www.asp.net/learn/3.5-extensions-videos/video-270.aspx.

     Any other thoughts?
     

  • Re: Issue in HandleUnknownAction method for custom Controller

    03-07-2008, 6:16 AM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 637
    • ASPInsiders

    tgmdbm:
    Of course,

    You're returning an InterceptionController which doesn't have ANY actions. So ALL actions will be Unknown.

    What you probably want to do is create a MyControllerBase class which overrides the HandleUnknownAction method. Then make all your controllers inherit from that and you're good to go.

     

    Watch the video again and don't listen to what Scott is saying.

     

    (Can someone back me up on this please?) 

  • Re: Issue in HandleUnknownAction method for custom Controller

    03-07-2008, 6:32 PM
    • Loading...
    • IMarshal
    • Joined on 07-30-2007, 3:56 PM
    • Posts 6

    Thanks again for the feedback.  So I agree with creating a new Controller class (let's call it AppController) derived from Controller and then define all of our application controller classes to in turn derive from AppController.  It is in AppController we can utilize the HandleUnknownAction handler to operate as I expect.  No problem with that.  However, I seem to be missing the point of his demo now using this InterceptionController.  Any thoughts?

  • Re: Issue in HandleUnknownAction method for custom Controller

    03-07-2008, 10:47 PM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 637
    • ASPInsiders

    Don't believe everything you hear just because the person saying it should know better. Even the grues get it wrong sometimes.

    Do you understand why HandleUnknownAction get's called for every action, even if it's a "known" action?

    Hint: I've already told you.

    Once you understand, you win. 

  • Re: Issue in HandleUnknownAction method for custom Controller

    03-08-2008, 2:07 PM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 637
    • ASPInsiders

    I just wanted to say, Scott (if you're reading this).

    I'm used to doing my own research and I don't instantly believe everything i'm told. Something i was trying to get across to IMarshal, but reading my comments again it sounded like i was taking a dig at you.

    I apologise for that.

    In fact, I hold you in the highest regard. As i've read your blog and listen to your podcast there's no doubt that you are the awesomeness that your colleagues purport.

  • Re: Issue in HandleUnknownAction method for custom Controller

    03-18-2008, 4:46 PM
    • Loading...
    • MeetNet
    • Joined on 03-12-2008, 5:13 PM
    • Posts 13

    Can someone post definitive sample code (with a base Controller and one that inherits from it) showing how HandleUnknownaction should behave?

     

    Thanks.

     

  • Re: Issue in HandleUnknownAction method for custom Controller

    03-18-2008, 5:47 PM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 637
    • ASPInsiders

    public abstract class MyBase : Controller { protected override void HandleUnknownAction(string actionName) { /* Do something here */ } }

    public class HomeController : MyBase { /* regular controller actions go here */ }
     
Page 1 of 1 (9 items)
Microsoft Communities
Page view counter