如何在ASP MVC中使用和保留Session变量 [英] How to use and persist Session variables in ASP MVC

查看:96
本文介绍了如何在ASP MVC中使用和保留Session变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP MVC应用程序,尝试在一个控制器中创建一个会话变量,然后在另一个控制器中访问该会话变量.我已经找到了有关PHP的文章,但是找不到ASP MVC的解决方案.

我的代码现在对一个控制器进行ajax调用以更新帐号:

  $ .ajax({类型:"PUT",网址:defender.techWebBaseUrl +"jobsinprogress/storenewmonitoringacctnumber/",数据:{acctNum:$(#newAcctNumber").val()}}); 

这在控制器上执行:

 公共无效的StoreNewMonitoringAcctNumber(string acctNum){Session ["MAN"] = acctNum;} 

成功创建会话变量的地方.稍后,在我的工作流中,在一个完全独立/不同的控制器中,我尝试访问相同的变量:

  .Configure(作业,类型,已发送",licenseStamp,EmployeeSignatureKey,Session ["MAN"].ToString()); 

但是每次Session变量为NULL时.我试图了解如何在MVC中持久保存Session变量,因为显然来自ASP.NET Web表单的相同规则不适用于此处.另外,保存Session变量然后尝试访问Session变量的这些动作必须存在于不同的控制器上,因此我绝对必须找到一种方法来持久保存该变量.

任何建议都值得赞赏.

解决方案

您正在尝试在MVC上下文中使用Webforms心态,这根本无法工作...

很少需要像在Webforms中使用viewstate/session一样,在MVC中持久化"变量.在MVC中,通常是通过使用强类型视图并将模型的实例传递到视图中,从而从那里访问变量来完成的.

在极少数情况下,您可以使用TempData/ViewData/Session,但实际上不建议您使用它.

我建议您阅读有关MVC以及如何使用强类型视图的更多信息.此链接是一个好的开始.>

I'm working with an ASP MVC application and attempting to create a session variable in one controller and then later access that session variable in a different controller. I've found articles about this for PHP but I'm having trouble finding a solution for ASP MVC.

My code right now makes an ajax call to one controller to update an account number:

                $.ajax({
                    type: "PUT",
                    url: defender.techWebBaseUrl + "jobsinprogress/storenewmonitoringacctnumber/",
                    data: { acctNum: $("#newAcctNumber").val() }
                });

This executes on the controller:

  public void StoreNewMonitoringAcctNumber(string acctNum)
    {
        Session["MAN"] = acctNum;
    }

Which successfully creates the session variable. Later on in my workflow, in a completely separate/different controller I attempt to access this same variable:

.Configure(job, type, "sent", licenseStamp, EmployeeSignatureKey, Session["MAN"].ToString());

But every time that Session variable is NULL. I'm trying to understand how to persist Session variables in MVC because obviously the same rules from ASP.NET Web forms do not apply here. Also, these actions of saving the Session variable then attempting to access the Session variable must exist on different controllers so I absolutely must find a way to persist that variable.

Any advice is appreciated.

解决方案

You are trying to use webforms mentality in a MVC context, which won't work at all...

Rarely do you need to "persist" variables in MVC the same way you would do in webforms by using viewstate/session. In MVC it's usually done by using a strongly typed view and pass an instance of the model into the view thus accessing the variable from there.

In some rare cases you can use TempData/ViewData/Session but it's really not recommended for what you are doing.

I recommend you read up more on MVC and how to use strongly-typed view. This link is a good start.

这篇关于如何在ASP MVC中使用和保留Session变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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