如何将服务器会话从Razor MVC#传递到Angularjs?
我试图找出将服务器会话数据从MVC Razor应用程序传递到Angularjs的最佳方法 在ASP.net中,我们能够使用System.Web.UI.Page扩展一个类,创建一个字符串字典项,并用json序列化该数据,然后将该项传递给this.ClientScript.RegisterClientScriptBlock,但我无法遵循相同的路径,因为我使用的是Razor 我目前正在传递ViewBag.variableName并在ng init中设置值,但这并不理想。所以我想到了几个主意 设置一个angular服务来获取ashx处理程序页面,该页面使用angular controller将会话放入$scope.variable中,这样会更好吗?或者将会话传递给视图,然后以某种方式将其放入$scope 从MVC Razor中获取服务器会话变量的最佳方法是什么 好的,我在下面发布了我的答案,但是我仍然很难让会话进入我的多个控制器的范围。因为http.get是异步的,我无法将结果集设置为动态的如何将服务器会话从Razor MVC#传递到Angularjs?,c#,angularjs,session,razor,model-view-controller,C#,Angularjs,Session,Razor,Model View Controller,我试图找出将服务器会话数据从MVC Razor应用程序传递到Angularjs的最佳方法 在ASP.net中,我们能够使用System.Web.UI.Page扩展一个类,创建一个字符串字典项,并用json序列化该数据,然后将该项传递给this.ClientScript.RegisterClientScriptBlock,但我无法遵循相同的路径,因为我使用的是Razor 我目前正在传递ViewBag.variableName并在ng init中设置值,但这并不理想。所以我想到了几个主意 设置一个a
这就是我最终做这件事的方式。我只是不知道这是否是最好的做法。根据注释,我指定要发送到的会话项 1) 我创建了一个唯一的字典,将数据传递给会话变量
private Dictionary<string, List<string>> _sessionDict
= new Dictionary<string, List<string>>();
4) 我把字典添加到会话中
Session.Add("jsPass", _sessionDict);
5) 我创建了一个ashx处理程序页面来获取会话变量,这是我的函数
private void getSessionVariables()
{
Dictionary<string, string> sessionData = new Dictionary<string, string>();
// Retrieve Session that is stored in dictionary
Dictionary<string, List<string>> sessionDict =
_context.Session["jsPass"] as Dictionary<string, List<string>>;
if (sessionDict != null)
{
foreach (KeyValuePair<string, List<string>> entry in sessionDict)
{
// Set the Value of the item
foreach (string value in entry.Value)
{
sessionData.Add(entry.Key, value.ToString());
}
}
}
// _context is just my private HttpContext variable I set at the beginning of the class
utility.httpContentWrite(_context, sessionData);
}
6) 我创建了一个angular controller函数来调用服务,并将该项拉入一个$scope变量,我将该函数设置为一个变量,然后执行函数调用
var setSession = function () {
UtilityService.getSession().success(
function (results) {
$scope.session = results;
});
};
setSession();
7) 我现在可以访问我的变量了
{{session.test}
8) 您可以使用此选项查看已设置的所有项目
{{key}}:{{value}
9) 我能够让会话进入范围只要在页面控制器中创建一个专用操作,就可以在AngularJs代码中从服务器会话获取JavaScript Ajax请求的值
// Action to return the session value. This method should be inside a controller class.
public object GetSession(string sessionName)
{
return Session[sessionName];
}
// JavaScript code to retrieve session "TEST":
$http.get('action/url/' + "TEST").
success(function (sessionValue) {
alert('Session "TEST" has the following value: ' + sessionValue);
}
);
您希望传递的会话数据类型的示例会有所帮助。公开这些物品从来都不是一个好主意(安全风险)。简单的值可以像您所做的那样注入到页面元素中。更复杂的数据可以(不推荐)作为JS文本对象注入。这完全取决于你想做什么。那么,创建一个专门用于传递给angular的会话数组会是一个更好的主意吗?你想从会话中获得什么样的数据到页面中?你能举个例子,让我那可怜的疲惫的大脑好好思考一下吗?:)您使用会话是否有特定的原因?MVC应该能够使用ViewData和TempData处理大多数数据传递。@DavidP我正试图找到一种更干净的方法,将数据传递到angular中,而不必将我所有的ViewBag推入ng init进行初始化。你可以分享一个例子吗?我对我的答案做了一个版本。请看它是否有用。
// Load Session
getSession: function () {
return $http.get('../../handlers/getSessionData.ashx');
}
var setSession = function () {
UtilityService.getSession().success(
function (results) {
$scope.session = results;
});
};
setSession();
<div ng-repeat="(key, value) in session">
<span>{{key}} : {{value}}</span>
</div>
// Action to return the session value. This method should be inside a controller class.
public object GetSession(string sessionName)
{
return Session[sessionName];
}
// JavaScript code to retrieve session "TEST":
$http.get('action/url/' + "TEST").
success(function (sessionValue) {
alert('Session "TEST" has the following value: ' + sessionValue);
}
);