[英]Saving current url in controller before navigating away from page
for some reasons I need to save the url from the page the user is navigating away from to use it in other views
and controllers
. 由于某些原因,我需要保存用户正在导航的页面的URL,以便在其他
views
和controllers
使用它。 IE: the user is on www.page1.com and is navigating to www.page2.com, then I want to save the url from www.page1.com somehow. IE:用户位于www.page1.com上,并且正在导航至www.page2.com,那么我想以某种方式保存来自www.page1.com的URL。
I already did some research and tried some things, first I was looking for cookies, but for a lot of reasons I want to ignore this possible solution. 我已经进行了一些研究并尝试了一些方法,首先,我正在寻找cookie,但是由于许多原因,我想忽略这种可能的解决方案。 Then I was looking for a solution which was using JQuery to set the MVC Session variables, which did not work because those where server side.
然后,我正在寻找一个使用JQuery设置MVC Session变量的解决方案,该解决方案因为在服务器端而无效。 At last I came up with a possible soluiton, which was making an AJAX call to the controller and using the UserProfile(which I did create in my app) to save the url being navigated away from.
最后,我想出了一个可能的解决方法,该方法是对控制器进行AJAX调用,并使用UserProfile(我确实在我的应用程序中创建过)来保存要从中导航的URL。
This is the code I was using: 这是我正在使用的代码:
The View: 风景:
$("a").on('click', function(e) {
$.ajax({
url: '<%= Url.Action("SetPreviousPage") %>',
type: 'POST',
data: $(location).attr('href');,
success: function() {
alert("Success");
},
error: function(xhr, status, error) {
alert(error.toString());
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
});
The controller: 控制器:
public JsonResult SetPreviousPage(string url)
{
FactsProfile.GetProfile().PreviousPage = url;
return Json(url);
}
For some reason this doesn't work, and I'm getting an error in my AJAX call in which I can't see what actually went wrong. 由于某种原因,这不起作用,并且我的AJAX调用出现错误,无法看到实际出了什么问题。
So two questions. 有两个问题。
1: Is this the right way of completing this task? 1:这是完成此任务的正确方法吗? Or is this to much work for such a small task?
还是为了这么小的任务而付出很多努力? 2: What is going wrong in my method?
2:我的方法出了什么问题? And how can I check where the problem came from.
以及如何检查问题的根源。
您是否尝试过使用Request.UrlReferrer,我记得它为您提供了进入该URL之前的URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.