跨多个控制器动作持久保存值 [英] Persisting values across multiple controller actions

查看:54
本文介绍了跨多个控制器动作持久保存值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器中,我有一个数据库中对象的ID,并且多个操作需要该ID才能访问该对象.该ID在Index操作中获取,并用于填充视图模型.我获取ID的方法是通过一个参数,该参数由CMS上的EPiServer传递Index动作,因此我无法在其他任何地方得到它.

In my controller i have an ID to an object in a database, and multiple actions needs this id to access the object. This ID is fetched in the Index action and used to populate the view model. The way I get the ID is through an argument, which EPiServer (out CMS) passes the Index action, so I cant really get it anywhere else.

我的问题是,有什么方法可以使我的操作可以访问此ID,而无需在客户端使用隐藏字段等?

My question is, is there any way to make this id accessible to my actions without the use a hidden field etc on the client side?

Id希望尽可能避免发送额外的数据,但这并不是一个很好的解决方案.

Id like to avoid sending extra data as much as possible, and it isnt really a good solution.

推荐答案

我不确定您的意思是这不是一个很好的解决方案",因为使用隐藏字段可能是最持久的跨数据持久化方法多个职位.请记住,HTTP是-应该被视为-无状态协议.但是,还可以考虑其他一些选择:

I'm not sure what you mean by "it isn't really a good solution," since using hidden fields is potentially the cleanest method of persisting data across multiple posts. Bear in mind that HTTP is - and should be treated as - a stateless protocol. A few other options to consider, however:

将值添加到控制器中的cookie,并将其添加到Response对象.然后,将其拾起并在需要时使用Request.Cookies进行阅读.

Add the value to a cookie in your controller and add it to the Response object. Then, pick it up and read it as and when you need it using Request.Cookies.

/* Setting */
Response.Cookies["id"].Value = "myId";

/* Getting */
var id = Request.Cookies["id"] != null
    ? Request.Cookies["id"].Value
    : String.Empty;

优点:可以跨多个请求,而不必在两个请求之间做任何事情.

Pros: Persist across multiple requests without having to do anything in the requests in between.

缺点:轻松关闭客户端.

使用Session集合将值添加到会话状态.

Add the value to session state using the Session collection.

/* Setting */
Session["id"] = "myId";

/* Getting */
var myId = Session["id"] != null ? Session["id"].ToString() : String.Empty;

优点:易于使用,可在多个请求中保持不变

Pros: Easy to use, persist across multiple requests

缺点:如果用户打开多个选项卡,使用返回"按钮等,可能会变得非常混乱.通常是不可预测的.

Cons: Can get very messy if the user has multiple tabs open, uses the 'Back' button, etc. Often unpredictable.

将值添加到隐藏字段中,并在下一个POST中选择该值.

Add the value to a hidden field and pick this value back up on the next POST.

/* View (assuming string model property 'Id') */
@Html.BeginForm("MyAction", "MyController")
{
    @Html.HiddenFor(m => m.Id)
    <input type="submit" value="Submit" />
}

/* Controller */
public ActionResult MyAction(MyModel model)
{
    var id = model.Id;
}

优点:没有足迹.干净,简单,无状态且高效.

Pros: No footprint. Clean, simple, stateless and efficient.

缺点:不能在GET请求中持久保存.一次只能一次保留一个POST.

Cons: No good for persisting across GET requests. Only persists across one POST at a time.

将这些值添加为重定向中的查询字符串参数.

Add the values as query string parameters on redirects.

/* View */
@Html.ActionLink("Go!", "MyAction", new { identifier = "myId" })

/* Controller */
public ActionResult MyAction(string identifier)
{
    var id = identifier ?? String.Empty;
}

优点::与隐藏字段不同,可用于GET请求.

Pros: Unlike hidden fields, can be used for GET requests.

缺点::要求您实现一种在所有需要其值的请求中持久保存查询字符串的方法.

Cons: Requires you to implement a method of persisting the query string across all requests for which you need the values.

就您个人而言,我个人认为使用cookie可能是最好的选择.如果您希望持久处理多个请求而不必处理每个请求中的值(并且没有使用Session的可怕性),则cookie可能是最简单,最整洁的方法.

Personally, I'd say that - in your case - using cookies is probably the best option. If you want to persist across multiple requests without having to handle the value in every single request (and without the horribleness of using Session), cookies are likely to be the simplest and neatest approach.

这篇关于跨多个控制器动作持久保存值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆