c# - HttpContext.Current.Session 在调用 Web 方法时抛出了 System.NullReferenceException 类型的异常
问题描述
我正在尝试在 Ajax 中调用 webmethod,并且我想通过会话获取当前登录用户 ID。HttpContext.Current.Session 怎么抛出 System.NullReferenceException,我怎么能得到 HttpContext.Current.Session。这是我的代码。
function GetReportID() {
indicatorsIds = getSelectedIndicatorsIDs();
locationsIds = getSelectedLocationsIDs();
$.ajax({
type: "POST",
url: '<%= ResolveUrl("ChartEdit.aspx/GetIDForDownloadExcel") %>',
data: JSON.stringify({ ind: indicatorsIds, loc: locationsIds }),
contentType: "application/json",
dataType: "json",
success: function (data) {
//alert('Data: ' + data);
var data = JSON.parse(data.d)
console.log(data);
console.log(data.ClientID);
},
error: function (xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
}
这是 WebMethod 和代码。
public static string GetIDForDownloadExcel(string[] ind, string[] loc)
{
ChartEdit cd = new ChartEdit();
List<string> locationIDs = new List<string>(1024);
List<string> indicatorIDs = new List<string>(1024);
Guid clientID = Guid.NewGuid();
excelData.Ready = 0;
store.Add(clientID, excelData);
Thread thread = new Thread(() => cd.ExportToExcelDict(locationIDs, indicatorIDs,clientID));
thread.Start();
return JsonConvert.SerializeObject(new { ClientID = clientID, filename = "ExportToExcel" });
}
当它运行到以下代码时,会抛出 NullReference Exception。
public static int GetCurrentUserId()
{
return Convert.ToInt32(HttpContext.Current.Session[FujiXerox.Common.Constants.KEY_NAME_CURRENT_USER_ID]);
}
解决方案
推荐阅读
- cppyy - 错误:使用未声明的标识符“LP_c_uint”
- android - 接收 com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: 加载字节数据作为来自 Retrofit 的响应时出错
- python - 即使我给出了正确的目录,python文件也没有找到异常
- visual-studio-code - 如何创建一个没有任何扩展名的新 VS 代码窗口
- android - 您更喜欢哪个数据库用于遗产应用程序?
- python - 为什么内存使用量会根据列表函数的位置而有所不同?
- javascript - 反应手柄鼠标不移动延迟
- android - 您可以添加到 android 键盘字典的最大单词数
- javascript - 当屏幕分辨率发生变化时,div的位置正在变化
- php - 当使用 Livewire 在 Laravel Blade 中运行验证时,如何隐藏某些内容?