简体   繁体   English

使用TempData,Session或静态变量在同一控制器中的ActionResults之间共享相同的值是否更好?

[英]Is it better to use TempData, Session, or a static variable to share the same value between ActionResults in the same controller?

I have an MVC controller and I wish to pass the same (static) information to any ActionResult in the same controller, changeable only by a new choice in the index page by the same user. 我有一个MVC控制器,我希望将相同的(静态)信息传递给同一控制器中的任何ActionResult,只能由同一用户在索引页面中的新选项进行更改。 I have read that it is considered bad practice to use a static variable. 我已经读过使用静态变量被认为是不好的做法。 My sites use Windows Authentication in an Intranet environment, and up to 10 people can be looking at any one page at any time. 我的网站在Intranet环境中使用Windows身份验证,任何时候最多可以有10个人查看任何一个页面。 If I understand what I read correctly, there is a danger that the static variable can be overwritten by someone other than the page user, simply by viewing the same page at the same time. 如果我理解我正确阅读的内容,则存在一个危险,即静态变量可以被页面用户以外的其他人覆盖,只需同时查看同一页面即可。

As an alternative, I read about "TempData" and "Session Variables", but so far I have not read anything indicating whether these approaches would ensure that the variable is set in the Index page by only the person viewing that instance of the page. 作为替代方案,我读到了“TempData”和“Session Variables”,但到目前为止,我还没有看到任何指示这些方法是否能确保变量在Index页面中仅由查看该页面实例的人设置的内容。 I have pasted code samples below that show a general idea of what I mean. 我在下面粘贴了代码示例,显示了我的意思。 I have gotten them to work, my question is which method ensures that only the person viewing that instance of the page sets and reads the value? 我让他们工作,我的问题是哪种方法确保只有查看该页面实例的人设置并读取值?

This code sample shows use of a controller-level static variable: 此代码示例显示了控制器级静态变量的使用:

public class HomeController : Controller
{
    public static string _currentChoice;
    public ActionResult Index(string CurrentChoice)
    {
        _currentChoice = string.IsNullOrEmpty(CurrentChoice)?"nothing":CurrentChoice;
        ViewBag.Message = "Your choice is " + _currentChoice;
        return View();
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your choice is still "+_currentChoice;
        return View();
    }
}

This code sample uses TempData and Session : 此代码示例使用TempDataSession

public class HomeController : Controller
{     
    public ActionResult Index(string CurrentChoice)
    {
        var _currentChoice = CurrentChoice;
        _currentChoice = string.IsNullOrEmpty(CurrentChoice)?"nothing":CurrentChoice;
        TempData["CurrentChoice"] = _currentChoice;
        Session["SessionChoice"] = _currentChoice;
        ViewBag.Message = "Your choice is " + _currentChoice;
        return View();
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your choice is still " + TempData["CurrentChoice"]
            + "\nYour Session choice is " + Session["SessionChoice"]; 
        return View();
    }
}

You'll want to use the Session. 你会想要使用Session。 Each of the options you presented have different use cases: 您提供的每个选项都有不同的用例:

  • Static variables use the same variable for every instance of the class. 静态变量对类的每个实例使用相同的变量。 Meaning every user will see the same value, and if one user changes the variable, it changes for all the others. 这意味着每个用户都会看到相同的值,如果一个用户更改了该变量,则所有其他用户都会更改。 Since you want it to be unique per user, this is not an option. 由于您希望每个用户都是唯一的,因此这不是一个选项。
  • TempData is for passing data during redirects, according to this answer 根据这个答案TempData用于在重定向期间传递数据
  • Session data is for storing data for the current session, and will be unique per user. 会话数据用于存储当前会话的数据,并且对于每个用户将是唯一的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在同一控制器中的两个ActionResult之间传递数据,从而在ASP.NET MVC中呈现不同的视图 - Passing data between two ActionResults in the same controller rendering different views in asp.net mvc C#在两个控制器ActionResults之间丢失值 - C# losing value between two controller ActionResults 如何在同一视图中的同一个控制器中执行2个不同的ActionResults? - How do I perform 2 different ActionResults in the same controller within the same view? 如何在回发之间和没有会话的情况下共享变量值 - How can I share a variable value between postbacks and without session 使用数据库值调用同名变量 - Use a database value to call a variable with the same name 多个WCF服务合同以共享同一会话 - Multiple WCF service contracts to share same session 在不同的线程中为静态变量赋值相同,还需要锁吗? - assign same value to static variable in different thread, still need lock? 会话变量考虑性能优于静态变量? 如果没有,解决方案是什么? - Session variable better than static variable considering performance? If not, what is the solution? 如何在同一个.cs文件中使用ISession和Session变量并在HTTPHandler中传递Session变量? - How to use ISession and Session Variable in same .cs file and pass Session variable inHttpHandler? 如何在同一服务器上托管的两个网站中共享同一会话? - How to share same session in two web sites hosted on same server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM