I'd like to know if it's possible to know which Controller+Action has been requested from the server and a way to act prior to serving it. Basically what I mean is, does the ASP.NET Routing have an event of sorts that gets fired whenever any route is requested?
Let's say a client requests the server for /Home/Index, I'd like to know, before the code goes to that Action, if /Home/Index was requested.
Thank you Ignatandrei but I'm looking more at the Route level or deeper (HttpHandler?). I don't want the user of my application to have to decorate with attributes the Action or Controller.
I think the answer to your question is "no, not really".
Routing goes thru its list of routes and the first match is used to execute a route handler which in turns produces a http handler. In the case of MVC all of those routes produce the same type of htp handler which then takes the controller route paramater
to locate, create and invoke your controller. Once you're into the contorller base class, then the action route paramater is used to find the action method. Model binding, validation, and filters are then invoked before the action is executed.
So if you really wanted to hook into this infrastructure, you could create your own API to register routes and that could use your own route handler. Your route handler could then simply defer to the MVC http handler. This might give you the hook you're
looking for before the MVC code gets rolling.
I am not so sure, but I feel that you can create custom "ActionFilterAttribute" attributes to take the advantage of ,what we call, a pre-action method call.
Yes, you can create an
ActionFilterAttribute derived class and there's an OnActionExecuting method you can override -- this will get involked before any action method but only if authorization has been successful (IOW, the client is not denied access). So it is a hook before
the action method, but it's not as early in the infrastructure as you said you wanted from the original post. The action filter is after the controller has been chosen and it's past the routing infrastructure, but maybe it is sufficient for your needs?
Ok I see what you're tryign to do. Well, if you want different URLs for each language/culture then you need to do this at the routing level. But if all you want is to know which language the user wants and then render different makrup, then you could implement
a HttpModule to set the culture. In other words, the HttpModule approach doesn't need to have different URLs for each culture -- you can determine the culture either form a HTTP header (Accept-Language) or if the user is logged in then maybe some setting in
the database.
Now I've just got to figure out how I can send a 404 instead of a 301 (yes, I know "just put 404!" but it doesn't work). Any of you have experience on this?
humble-appre...
Member
32 Points
73 Posts
Detecting which Controller+Action has been requested to the server
Mar 19, 2012 07:22 PM|LINK
Hello!
I'd like to know if it's possible to know which Controller+Action has been requested from the server and a way to act prior to serving it. Basically what I mean is, does the ASP.NET Routing have an event of sorts that gets fired whenever any route is requested?
Let's say a client requests the server for /Home/Index, I'd like to know, before the code goes to that Action, if /Home/Index was requested.
Thanks!
asp.netmvc
humble-apprentice
ignatandrei
All-Star
137698 Points
22155 Posts
Moderator
MVP
Re: Detecting which Controller+Action has been requested to the server
Mar 19, 2012 08:58 PM|LINK
http://bradwilson.typepad.com/blog/2010/07/aspnet-mvc-filters-and-statefulness.html
humble-appre...
Member
32 Points
73 Posts
Re: Detecting which Controller+Action has been requested to the server
Mar 19, 2012 10:06 PM|LINK
Thank you Ignatandrei but I'm looking more at the Route level or deeper (HttpHandler?). I don't want the user of my application to have to decorate with attributes the Action or Controller.
Appreciate your help!
humble-apprentice
BrockAllen
All-Star
28084 Points
4997 Posts
MVP
Re: Detecting which Controller+Action has been requested to the server
Mar 20, 2012 03:20 AM|LINK
I think the answer to your question is "no, not really".
Routing goes thru its list of routes and the first match is used to execute a route handler which in turns produces a http handler. In the case of MVC all of those routes produce the same type of htp handler which then takes the controller route paramater to locate, create and invoke your controller. Once you're into the contorller base class, then the action route paramater is used to find the action method. Model binding, validation, and filters are then invoked before the action is executed.
So if you really wanted to hook into this infrastructure, you could create your own API to register routes and that could use your own route handler. Your route handler could then simply defer to the MVC http handler. This might give you the hook you're looking for before the MVC code gets rolling.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
ramankashyap
Member
178 Points
43 Posts
Re: Detecting which Controller+Action has been requested to the server
Mar 21, 2012 04:17 PM|LINK
I am not so sure, but I feel that you can create custom "ActionFilterAttribute" attributes to take the advantage of ,what we call, a pre-action method call.
BrockAllen
All-Star
28084 Points
4997 Posts
MVP
Re: Detecting which Controller+Action has been requested to the server
Mar 21, 2012 04:55 PM|LINK
Yes, you can create an ActionFilterAttribute derived class and there's an OnActionExecuting method you can override -- this will get involked before any action method but only if authorization has been successful (IOW, the client is not denied access). So it is a hook before the action method, but it's not as early in the infrastructure as you said you wanted from the original post. The action filter is after the controller has been chosen and it's past the routing infrastructure, but maybe it is sufficient for your needs?
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
humble-appre...
Member
32 Points
73 Posts
Re: Detecting which Controller+Action has been requested to the server
Mar 21, 2012 04:59 PM|LINK
I'm still investigating. Initially I was using this:
http://www.simple-talk.com/dotnet/.net-framework/asp.net-mvc-routing-extensibility/
but this looks pretty much like what I want to do:
http://blog.jeroenverhulst.be/2010/06/14/extending-mvcroutehandler-to-enable-localization-of-an-asp-net-mvc2-application/
I don't know if I have to give up yet and use the attributes
humble-apprentice
BrockAllen
All-Star
28084 Points
4997 Posts
MVP
Re: Detecting which Controller+Action has been requested to the server
Mar 21, 2012 05:12 PM|LINK
Ok I see what you're tryign to do. Well, if you want different URLs for each language/culture then you need to do this at the routing level. But if all you want is to know which language the user wants and then render different makrup, then you could implement a HttpModule to set the culture. In other words, the HttpModule approach doesn't need to have different URLs for each culture -- you can determine the culture either form a HTTP header (Accept-Language) or if the user is logged in then maybe some setting in the database.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
humble-appre...
Member
32 Points
73 Posts
Re: Detecting which Controller+Action has been requested to the server
Mar 25, 2012 12:47 AM|LINK
This looks even closer to what I want!
http://antix.co.uk/Blog/301-Redirects-in-ASP.NET-MVC-updated-for-MVC3
Now I've just got to figure out how I can send a 404 instead of a 301 (yes, I know "just put 404!" but it doesn't work). Any of you have experience on this?
Thanks!
IRouteHandler
humble-apprentice
humble-appre...
Member
32 Points
73 Posts
Re: Detecting which Controller+Action has been requested to the server
Mar 27, 2012 01:25 PM|LINK
This is the only thing I was able to come up with:
throw new HttpException(404, httpContext.Request.Url.AbsoluteUri + "was not found");
humble-apprentice