简体   繁体   English

离开页面之前,将当前URL保存在控制器中

[英]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,以便在其他viewscontrollers使用它。 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.

相关问题 离开页面时记住级联下拉值 - Remember Cascading Dropdown value when navigating away from page 导航离开某个页面时执行c#命令 - Executing c# commands when navigating away from a certain page 离开一页时如何清除会话 - How to clear Session when navigating away from one page 在Silverlight中,将导航远离包含线程的页面结束线程吗? - In Silverlight, will navigating away from the page that contains a thread end the thread? 导航并返回页面后,获取背景音频的当前播放时间-UWP - Get current playback time of background audio after navigating away and back to the page - UWP 如何获取页面当前导航至的URL(导航完成之前) - How to get the URL that the page is currently navigating to (Before navigation finishes) UWP - 如果在async / await运行时离开页面会发生什么? - UWP - What happens if navigating away from page while async/await is running? Xamarin 滑块:“值是最大值的无效值”(仅当导航离开页面时) - Xamarin Slider: 'Value is an invalid value for Maximum' (Only when navigating away from a page) 从C#(.Net 3.5)页面导航时调用方法的方法? - Way to call a method when navigating away from a page in C# (.Net 3.5)? 从网址获取当前页面 - get current page from url
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM