4

I have a site which needs to track referrals from other registered affiliate sites. Each of these partners can redirect to any page within my site, and have to append their tracking parameters onto the querystring e.g:

http://www.somesite.com/Home/About?i=ABCDEFG&t=65DCEFC2-2B73-429C-BA23-C824BFD01844

On each request I want the site to first check whether an 'Introducer' object has been stored in the session. If not then check for the existence of these params and save the corresponding Introducer object (or use a default if they are not provided). I currently only have this implemented in selected controllers / actions and am calling the code manually however would like it to happen automatically - would a custom route handler be the best for this? or simply overriding OnActionExecuting in a controller base class and handling here?

Macros
  • 7,099
  • 2
  • 39
  • 61
  • 1
    If your requirement is for *every* request, seems to me the best place to handle it would be in a custom route handler, since you would have to inherit a base class for every controller otherwise. – Robert Harvey Jan 10 '12 at 16:35
  • @Robert Harvey - Is there a downside to inheriting a base class for every controller? – Macros Jan 10 '12 at 16:37
  • 1
    Mostly inconvenience. From a best-practices standpoint, it's a routing issue, and should be handled in the routing. – Robert Harvey Jan 10 '12 at 16:38
  • Url parameters are usually designated optional in routes. Couldn't you just make them non-optional, and then have a catch-all route at the bottom that goes to an error page? – Robert Harvey Jan 10 '12 at 16:41
  • They are optional though – Macros Jan 10 '12 at 16:41
  • So if I understand the question correctly, you have to actually hit your data store to validate these tracking parameters, correct? If that is the case, it would probably be cleaner to just inherit from a base controller. – Robert Harvey Jan 10 '12 at 16:45
  • That's right - thanks. I thought this was the case however so many ties have gone down one route only to find that there is already a more 'accepted' way to do something built into the framework – Macros Jan 10 '12 at 16:47

1 Answers1

3

Create an Action Filter.

You can access all of the route data in the action filter and do whatever you need to do there. You could set it up as a global filter in your Global.asax:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new CustomFilter());
}
Dismissile
  • 32,564
  • 38
  • 174
  • 263
  • Yes... This is probably the right approach. There is an example of creating a custom `AuthorizationFilter` here: http://stackoverflow.com/questions/2322366 – Robert Harvey Jan 10 '12 at 16:47
  • Registering as a global filter...that's the bit I was missing, thanks! – Macros Jan 10 '12 at 16:49