简体   繁体   English

ASP.NET MVC中的全局路由参数

[英]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. 我无法执行AJAX请求,因为如果他们单击链接,该页面将被卸载(因为他们将转到新页面),并且我不想强迫他们停留在该页面上,直到发生同步事件为止饰面。

So one idea I had was to add a parameter to the url. 因此,我的一个想法是向网址添加参数。 Ie the urls would be actual/action?actualParams&infoIWantToLog=data . 也就是说,网址将是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. 然后,我可以剥离要记录的信息,进行记录,然后将其URL传递给他们实际想要执行的操作。 Is this possible to do with MVC routing? 这可能与MVC路由有关吗?

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. 通常,跟踪URL将用户发送到特定页面(在这种情况下为操作),例如/ Track,然后通过查询字符串将参数传递到日志(包括发送用户的URL)。

For instance, go to Google and search on a term. 例如,转到Google并搜索一个词。 Right click on a result and copy the URL. 右键单击结果,然后复制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. 您会发现它不是直接链接到搜索结果,而是链接到Google跟踪页面。 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: 例如,当我搜索asp.net并单击第一个结果(对于www.asp.net)时,这就是Google发送给我的实际URL:

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 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. 该页面记录了我的点击,然后将我重定向到www.asp.net网站。

Your Track action would do the same thing. 您的Track动作将执行相同的操作。 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. 您将使用Index动作创建一个Track控制器,然后在其中解析查询字符串,记录信息,然后将RedirectResult返回到要发送给用户的URL。

Make sense? 说得通?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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