如何从查询字符串中隐藏参数 (url) ASP.NET

How to hide parameters from querystring (url) ASP.NET

我试图在我的 Web 应用程序中隐藏查询字符串中的参数。 我已经能够通过使用会话来存储临时变量来做到这一点。所以它会像这样工作:

1.单击查看配置文件按钮:

href="@Url.Action("RedirectWithId", "Redirect", new { act = "ProfileView", ctrl = "User", id = member.Id})"

2。调用重定向方法并存储临时数据:

public class RedirectController : Controller
{
    public ActionResult RedirectWithId(string act, string ctrl, int id)
    {
        Session["temp_data"] = id;
        return RedirectToAction(act, ctrl);
    }
}

3。在没有参数的action方法中使用它:

public ActionResult ProfileView()
    {
        if (Session["temp_data"] == null)
        {
            return Redirect(Request.UrlReferrer.ToString());
        }

        int id = (int)Session["temp_data"];
        var model = GetUserById(id);

        return View(model);
    }

所以它工作得很好,但是,这种隐藏参数的方法不能处理假设我转到第一个配置文件(id 4),然后转到第二个配置文件(id 8)的情况。如果从第二个配置文件我按下导航器上的后退按钮试图返回到第一个配置文件(id 4),我将被重定向到当前配置文件(id 8),因为 8 是当前值Session["temp_data"].

有没有办法处理这种特殊情况?或者是另一种完全不同且更好的隐藏参数的方法 URL?

谢谢!

你可以试试这个而不是 Session

TempData["temp_data"]

我得出的结论是,由于我已经在我的应用程序中使用了授权和角色,所以我不需要总是隐藏参数。每当我将复杂对象作为参数传递时,我都可以简单地隐藏。