Last post Mar 27, 2012 02:25 PM by humble-apprentice
Mar 19, 2012 08:22 PM|humble-apprentice|LINK
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.
Mar 19, 2012 09:58 PM|ignatandrei|LINK
Mar 19, 2012 11:06 PM|humble-apprentice|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!
Mar 20, 2012 04:20 AM|BrockAllen|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.
Mar 21, 2012 05:17 PM|ramankashyap|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.
Mar 21, 2012 05:55 PM|BrockAllen|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?
Mar 21, 2012 05:59 PM|humble-apprentice|LINK
I'm still investigating. Initially I was using this:
but this looks pretty much like what I want to do:
I don't know if I have to give up yet and use the attributes
Mar 21, 2012 06:12 PM|BrockAllen|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
Mar 25, 2012 01:47 AM|humble-apprentice|LINK
This looks even closer to what I want!
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?
Mar 27, 2012 02:25 PM|humble-apprentice|LINK
This is the only thing I was able to come up with:
throw new HttpException(404, httpContext.Request.Url.AbsoluteUri + "was not found");