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
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;
}
// 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));
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?
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.
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'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.
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?
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.
public abstract class MyBase : Controller { protected override void HandleUnknownAction(string actionName) { /* Do something here */ } }
public class HomeController : MyBase { /* regular controller actions go here */ }
IMarshal
0 Points
6 Posts
Issue in HandleUnknownAction method for custom Controller
Mar 06, 2008 05:19 AM|LINK
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!
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: Issue in HandleUnknownAction method for custom Controller
Mar 06, 2008 12:01 PM|LINK
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.
IMarshal
0 Points
6 Posts
Re: Issue in HandleUnknownAction method for custom Controller
Mar 06, 2008 08:23 PM|LINK
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?
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: Issue in HandleUnknownAction method for custom Controller
Mar 07, 2008 10:16 AM|LINK
Watch the video again and don't listen to what Scott is saying.
(Can someone back me up on this please?)
IMarshal
0 Points
6 Posts
Re: Issue in HandleUnknownAction method for custom Controller
Mar 07, 2008 10:32 PM|LINK
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?
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: Issue in HandleUnknownAction method for custom Controller
Mar 08, 2008 02:47 AM|LINK
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.
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: Issue in HandleUnknownAction method for custom Controller
Mar 08, 2008 06:07 PM|LINK
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.
MeetNet
Member
10 Points
13 Posts
Re: Issue in HandleUnknownAction method for custom Controller
Mar 18, 2008 08:46 PM|LINK
Can someone post definitive sample code (with a base Controller and one that inherits from it) showing how HandleUnknownaction should behave?
Thanks.
handleunknownaction base controller icontroller
tgmdbm
Contributor
4392 Points
883 Posts
ASPInsiders
MVP
Re: Issue in HandleUnknownAction method for custom Controller
Mar 18, 2008 09:47 PM|LINK
public abstract class MyBase : Controller { protected override void HandleUnknownAction(string actionName) { /* Do something here */ } } public class HomeController : MyBase { /* regular controller actions go here */ }