简体   繁体   中英

Global routing parameter in ASP.NET MVC

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. Ie 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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM