重定向后获取隐藏 URL 参数

发布于 2024-12-20 19:43:53 字数 867 浏览 4 评论 0原文

让我先尝试在这里设置场景。这是使用 ASP.NET 4.0、MVC3 和 C# 完成的。

我有一个画廊。我有一个图片库相册的管理员。每个画廊都有一组页面。

当我使用相册管理器时,视图显示当前相册的列表。

在相册列表中,可以选择编辑、删除或管理该相册中的页面。

当选择管理相册的页面时,会出现一个页面列表,每个页面都具有编辑、删除和查看功能。

当选择页面的编辑按钮并进行编辑然后保存时,遇到了我的问题。

从相册列表进入要管理的页面列表的按钮通过 post 发送 id 以隐藏专辑 ID 和其他参数。

因此页面的视图是从 [HttpPost] 控制器生成的。然后,从管理页面的视图中,当单击编辑按钮时,出于相同的原因(隐藏参数),会从 [HttpPost] 控制器生成用于编辑的页面。

问题在于:在编辑视图中单击保存按钮后,数据将通过 [HttpPost] 发布到保存更改控制器,但我不想返回编辑视图(刷新会重新发布更改,这并不是最好的选择)实践)。我想返回所选专辑的页面列表。在此过程中,我使用编辑控制器中的 [HttpPost] 重定向回 [HttpGet] 控制器,以获取列出所选相册页面的视图。当我这样做时,调用看起来像这样:

return RedirectToAction("ManagePages", new { albumId = model.AlbumId });

不幸的是,这使得 url 显示了相册 id: http://XXX /ManagePages?AlbumId=56

如何返回同一选定相册的 ManagePages 视图,而不在参数列表中显示相册 ID?

Let me try to set the scenario here first. This is done using ASP.NET 4.0, MVC3, and C#.

I have a picture gallery. I have a manager for the albums in the picture gallery. Each gallery has a set of pages.

When I am using the manager for the albums, the view shows a list of current albums.

In the list of albums, there are choices to edit, delete, or manage pages in that album.

When selecting to manage the pages for the album, there is a list of pages, each with edit, delete, and view.

When the edit button for the page is selected, and edits are made then saved, my problem is encountered.

The button to get to the list of pages to manage from the list of albums sends the id via post to hide the albumid and other parameters.

So the view for the pages is generated from a [HttpPost] controller. Then, from the view to manage pages, when the edit button is clicked, the page for edit is generated from a [HttpPost] controller for the same reason (to hide parameters).

Here is the problem: Once the save button is clicked in the edit view, data is posted to a save changes controller via [HttpPost], but I do not want to return the edit view (refresh would repost changes and it is not really best practice). I want to return to the list of pages for the selected album. In doing this, I am using [HttpPost] from the edit controller to redirect back to the [HttpGet] controller for the view that lists the pages for the selected album. When I do this, the call looks like this:

return RedirectToAction("ManagePages", new { albumId = model.AlbumId });

This unfortunately makes the url have the album id shown: http://XXX/ManagePages?AlbumId=56.

How can I return to the ManagePages view for the same selected album without showing the album id in the parameter list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃扇骨 2024-12-27 19:43:53

您可以在执行重定向之前将相册 ID 存储在 TempData 中:

TempData["AlbumId"] = model.AlbumId;
return RedirectToAction("ManagePages");

然后在 ManagePages GET 操作中您可以读取 id 的内容:

int albumId = (int)TempData["AlbumId"];

注意:读取 id 后,TempData["AlbumId"] 的内容将被读取已清除。


更新:

实际上,如果您使用 TempData 并且用户单击刷新,则刷新时数据将丢失。因此,您可以改用会话:

Session["AlbumId"] = model.AlbumId;
...
int albumId = (int)Session["AlbumId"];

You could store the album id in the TempData just before performing the redirect using:

TempData["AlbumId"] = model.AlbumId;
return RedirectToAction("ManagePages");

Then in the ManagePages GET action you could read the contents of the id:

int albumId = (int)TempData["AlbumId"];

Note: Once the id has been read the contents of TempData["AlbumId"] will be cleared.


Update:

Actually, if you use TempData and the user clicks refresh then the data will be lost on the refresh. Therefore, you could instead use the Session:

Session["AlbumId"] = model.AlbumId;
...
int albumId = (int)Session["AlbumId"];
做个ˇ局外人 2024-12-27 19:43:53

在执行 RedirectToAction 之前,您可以使用相册 ID 设置 cookie。然后您可以在 ManagePages 操作中读取它。

然而,值得指出的是,如果您对外部了解内部 ID 很敏感,那么您的应用程序仍然通过 HTML 源公开它们。其他用户可以使用它们来查看他们不应该查看的数据,除非您有某种身份验证机制。

Before doing the RedirectToAction, you can set a cookie with the album ID. Then you read it in the ManagePages action.

However, it is worth stating that if you are sensitive about internal IDs being known externally, your application still exposes them via the HTML source. Other users could use them to view data they are not suppose to unless you have some sort of authentication mechanism in place.

燕归巢 2024-12-27 19:43:53

您可以将 ID 隐藏在 TempData 变量中。 TempData 通过一次重定向持续存在。

You could hide the ID in a TempData variable. TempData persists through one redirect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文