首页 > 解决方案 > How can I delete unavailable URL in browser history using C# .NET?

问题描述

I'm building an animal feed supplying website using C# .NET

It has those features below: http://localhost:52000/Account/Index =>show a list of accounts (ID, name...).

Click on an ID from Index page, it leads into Detail page: http://localhost:52000/Account/Details/6cc608a5-3b4b-4c6f-b220-3422c984919a

In account detail's page, it also has 2 buttons(functions): Delete account and Edit account information.

All I want is after deleting an account (in Detail view), website will redirect to the previous available page (Index,...). Therefore I use window.location.href = "/Account/Index/"; in Delete function.

Here is my delete function with redirecting solution:

function deleteAccount(id) {
            var data = { 'id': id };
            $.ajax({
                *//....*

                success: function (result) {
                    if (result) {
                         *//redirect to the previous page (Index)*
                         window.location.href = "/Account/Index/";
                    }
                }
            });
        }

However,after deleting and redirecting to "/Account/Index/" successfully, if Admin click on Back Button on Browser, website redirect to unavailable page (the Detail page of that deleted account: http://localhost:52000/Account/Detail/6cc608a5-3b4b-4c6f-b220-3422c984919a).

Then I tried to use window.history.back();, window.history.go(-1);, window.location.replace("/Account/Index/"); in turn instead, it worked perfectly only when Admin just deletes that account, if Admin Edits this account first then updates then deletes (Press Edit in Detail view -> Go to Edit view -> press Update -> Go back to Detail View ) --> website redirect to unavailable page (the editing page of that deleted account: http://localhost:52000/Account/Edit/6cc608a5-3b4b-4c6f-b220-3422c984919a).

function deleteAccount(id) {
            var data = { 'id': id };
            $.ajax({
                *//....*

                success: function (result) {
                    if (result) {
                         *//redirect to the previous page (Index)*
                         window.history.back(); 
                         // or window.history.go(-1)
                         //or window.location.replace("/Account/Index/");
                    }
                }
            });
        }

Is that possible to remove the unavailable URLs (those include ID of deleted account) in browser? How can I handle the Back Button in Browser to go through those unavailable URLs? (http://localhost:52000/Account/Detail/6cc608a5-3b4b-4c6f-b220-3422c984919a and http://localhost:52000/Account/Edit/6cc608a5-3b4b-4c6f-b220-3422c984919a)

标签: c#asp.netasp.net-mvcbrowser-history

解决方案


您可以尝试以下方法:

window.location.replace("/Account/Index/");

这相当于使用 Javascript 的 HTTP 重定向。

当您使用window.location.href它时,就好像用户单击了一个链接,因此您可以在之后返回到以前的 URL。


推荐阅读