views:

20

answers:

1

When a user clicks on a link, I want to log some information. I can't do an AJAX request because if they clicked on a link, the page will unload (since they're going to a new page), and I don't want to force them to stay on the page until a synchronous event finishes.

So one idea I had was to add a parameter to the url. I.e. the urls would be actual/action?actualParams&infoIWantToLog=data. Then I could strip off the info I want to log, log that, and then pass off their URL to the action which they actually wanted to go to. Is this possible to do with MVC routing?

+2  A: 

Typically tracking URLs send the user to a specific page (or action, in this case), say, /Track, and then pass the parameters to log (including the URL to send the user to) through the querystring.

For instance, go to Google and search on a term. Right click on a result and copy the URL. Now paste it into Notepad. You'll see that it's not a direct link to the search result, but rather to a Google tracking page. For instance, when I search for asp.net and click on the first result (for www.asp.net), this is the actual URL Google sends me to:

http://www.google.com/url?sa=t&source=web&cd=2&ved=0CDcQFjAB&url=http%3A%2F%2Fwww.asp.net%2Fget-started&rct=j&q=asp.net&ei=BauTTIuCDIaWsgOHgo3ACg&usg=AFQjCNGR6cOBKtUWIKZs9jnpz0vYRDu_EA&sig2=r2q31zniuxHQ4Y6BIoudow&cad=rja

That page logs my click and then redirects me to the www.asp.net website.

Your Track action would do the same thing. You'd create a Track controller with an Index action and then in there you'd parse the querystring, log the information, and then return a RedirectResult to the URL you want to send the user.

Make sense?

Scott Mitchell
I guess that makes sense, though it's kind of annoying I can't just handle this with routing. Thanks!
Xodarap
@Xodarap: I guess I'm not understanding what you mean by "just handle this with routing." Routing is just a way to map an incoming URL to an action in a controller.
Scott Mitchell
@Scott: I want to map the URL into two actions (track and the actual action). Maybe routing is a bad paradigm; a better example might be the security attributes you can decorate actions with. Security and logging are both cross-cutting concerns, and so it would be nice if there was a simple clean way to deal with them both.
Xodarap