问题描述
我使用的是在我的ASP.NET MVC应用程序模式之后。我有
以下情形:
I am using the Redirect After Post pattern in my ASP.NET MVC application. I havethe following scenario:
- 用户进入
/控制器/指数
在那里他被要求填写的表格。 - 表单值发布到
/控制器/计算
。 - 的
计算
操作执行基于输入计算和实例包含操作的结果,一个复杂的对象。这个对象存储在的TempData code>键,用户被重定向到
/控制器/结果
。 -
/控制器/结果
从的TempData code>检索结果,并将其呈现给用户。
- User goes to
/controller/index
where he is asked to fill a form. - Form values are POSTed to
/controller/calculate
. - The
Calculate
action performs calculation based on input and instantiates a complex object containing the results of the operation. This object is stored inTempData
and user is redirected to/controller/result
. /controller/result
retrieves the result fromTempData
and renders them to the user.
使用这种方法的问题是,如果用户点击F5而在 /控制器/结果
页面不再被呈现为 TempData的已过期,结果对象不再可用。
The problem with this approach is that if the user hits F5 while viewing the results in /controller/result
the page can no longer be rendered as TempData
has been expired and the result object is no longer available.
此行为不是由用户需要的。一个可能的解决办法是,而不是POST之后重定向,只呈现结果视图。现在,如果用户点击F5他得到一个浏览器对话框,询问他是否要重新发布的形式。这也被不希望的。
This behavior is not desired by the users. One possible solution would be instead of redirecting after the POST, just rendering the results view. Now if the user hits F5 he gets a browser dialog asking if he wants to repost the form. This also was not desired.
一个可能的解决办法,我认为是序列化的结果对象,并传递给它的URL重定向之前,但据我所知有一些限制在GET请求的长度,如果对象获得pretty大我可能会打这个限制(特别是如果连接的base64 codeD)。
One possible solution I thought of was to serialize the result object and passing it in the URL before redirecting but AFAIK there are some limitations in the length of a GET request and if the object gets pretty big I might hit this limitation (especially if base64 encoded).
另一种可能性是使用会话
对象,而不是的TempData code>来坚持的结果。但是实施这一解决方案之前,我想知道是否有这样做的更好的方法。
Another possibility would be to use the Session
object instead of TempData
to persist the results. But before implementing this solution I would like to know if there's a better way of doing it.
更新:
进一步调查的问题,我发现,如果我重新把结果对象的TempData code>的
/控制器/结果里面
操作它的实际工作:
Further investigating the issue I found out that if I re-put the result object in TempData
inside the /controller/result
action it actually works:
public ActionResult Result()
{
var result = TempData["result"];
TempData["result"] = result;
return View(result);
}
但这种感觉那种脏。难道还有任何副作用,这种方法(如切换到进程外会话提供商目前我使用的是InProc)?
But this feels kind of dirty. Could there be any side effects with this approach (such as switching to out-of-process session providers as currently I use InProc)?
推荐答案
储存于一些独特的关键会话并通过键作为URL的一部分。只要那么作为会议还活着,他们可以使用后退/前进按钮到他们的心脏的内容,仍然有URL正确响应。或者,您可以使用ASP缓存,但我通常保留,对于那些用户之间共享对象。当然,如果你使用的参数来计算的关键,你发现的结果在缓存中,你可以简单地重新使用它。
Store it in the Session with some unique key and pass the key as part of the url. Then as long as the session is alive they can use the back/forward button to their heart's content and still have the URL respond properly. Alternatively, you could use the ASP cache, but I'd normally reserve that for objects that are shared among users. Of course, if you used the parameters to the calculation as the key and you found the result in the cache, you could simply re-use it.
这篇关于重定向后在ASP.NET MVC后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!