【转】在ashx页面中context.Session["xxx"]获取不到值的解决办法

本文介绍了在ASP.NET中如何在不同的文件类型(如aspx、aspx.cs和ashx)中管理和使用Session状态。同时,文章还讲解了如何在AppCode的cs文件中通过HttpContext.Current来操作Session,并给出了获取客户端IP地址的具体实现。

IRequiresSessionState

From:http://www.cnblogs.com/wills2010/archive/2010/08/14/1799585.html


1.在 aspx和aspx.cs中,都是以Session["xxx"]="aaa"和aaa=Session["xxx"].ToString()进行读写。而在ashx中,Session都要使用context.Session,读写方法不变。


2.在ashx文件中,若要对Session进行成功的读写,应该在使用 Session的class后增加接口IRequiresSessionState (添加 时可能提示添加命名空间 using System.Web.SessionState 的引用),否则 .Sessio context n["xxx"]读出的总是null


那么,在aspx、aspx.cs和ashx中可以使用 Session后,我们在 AppCode的cs文件中,如何操作Session或者得到访问者的IP?

首先, aspx.cs中是直接Request和Session,而在ashx中是 context.Request和 context.Session。aspx.cs中可以直接使用,是因为有Web.UI的支持,而ashx中只能靠传进去的HttpContext实例对 Request和Session等进行操作。那么同样的,AppCode中的cs文件中也没有Web.UI,但是没有HttpContext参数,如果 cs中可以得到当前的HttpContext,那么自然而然就可以根据这个HttpContext去操作Request和Session。

HttpContext类中有一个静态属性叫Current,我们可以通过这个属性去得到当前的HttpContext。当然,在cs中要操作Session,也应该增加IRequiresSessionState 接口。


与Session、 Request、Response、Server等相关的方法和属性,都可以根据这个模式、方法去套用。

比如,在AppCode的cs文件中要得到当前访问者的IP,可以用HttpContext.Current.Request.UserHostAddress。


Public Function CreateExcel(ByVal ds As DataSet) As NPOI.SS.UserModel.IWorkbook '创建workbook Dim workbook As NPOI.SS.UserModel.IWorkbook = New NPOI.XSSF.UserModel.XSSFWorkbook() '创建sheet Dim sheet As NPOI.SS.UserModel.ISheet = workbook.CreateSheet("Sheet1") '寫列標題 Dim format1 As IDataFormat = workbook.CreateDataFormat() Dim dateCellStyle As ICellStyle = workbook.CreateCellStyle() dateCellStyle.DataFormat = format1.GetFormat("yyyy/MM/dd HH:mm:ss") Dim hlink_style As XSSFCellStyle = workbook.CreateCellStyle() Dim hlink_font As XSSFFont = workbook.CreateFont() hlink_font.Underline = FontUnderlineType.Single hlink_font.Color = HSSFColor.Blue.Index hlink_style.SetFont(hlink_font) Dim link As HSSFHyperlink '创建行 Dim SQLSTR As String Dim row As NPOI.SS.UserModel.IRow Dim iscost As String = "" Dim typeGroupNum As Integer = 0 For i As Integer = 0 To ds.Tables(1).Rows.Count - 1 Dim typeId As String = ds.Tables(1).Rows(i).Item("分類編號").ToString() Dim strType As String = "select * from MITDEVIL.料號_參數 where 分類編號='" + typeId + "' order by CAST(項目 AS INT )" Dim ds1 As DataSet = db.FillDataSet(strType) '创建行 row = sheet.CreateRow(i + typeGroupNum) row.CreateCell(0).SetCellValue(Convert.ToString("兄弟料號")) row.CreateCell(1).SetCellValue(Convert.ToString("公司編號")) row.CreateCell(2).SetCellValue(Convert.ToString("公司名稱")) row.CreateCell(3).SetCellValue(Convert.ToString("料號")) row.CreateCell(4).SetCellValue(Convert.ToString("階次1")) row.CreateCell(5).SetCellValue(Convert.ToString("階次2")) row.CreateCell(6).SetCellValue(Convert.ToString("階次3")) row.CreateCell(7).SetCellValue(Convert.ToString("階次4")) row.CreateCell(8).SetCellValue(Convert.ToString("階次5")) row.CreateCell(9).SetCellValue(Convert.ToString("階次6")) row.CreateCell(10).SetCellValue(Convert.ToString("階次7")) row.CreateCell(11).SetCellValue(Convert.ToString("階次8")) row.CreateCell(12).SetCellValue(Convert.ToString("階次9")) row.CreateCell(13).SetCellValue(Convert.ToString("階次10")) row.CreateCell(14).SetCellValue(Convert.ToString("類別編號")) row.CreateCell(15).SetCellValue(Convert.ToString("類別名稱")) row.CreateCell(16).SetCellValue(Convert.ToString("品名規格")) 'row.CreateCell(17).SetCellValue(Convert.ToString("單位")) row.CreateCell(18).SetCellValue(Convert.ToString("入庫幣別")) row.CreateCell(19).SetCellValue(Convert.ToString("入庫單價")) row.CreateCell(20).SetCellValue(Convert.ToString("價格冊幣別")) row.CreateCell(21).SetCellValue(Convert.ToString("價格冊價格")) row.CreateCell(22).SetCellValue(Convert.ToString("基準幣別")) row.CreateCell(23).SetCellValue(Convert.ToString("基準幣別單價")) row.CreateCell(24).SetCellValue(Convert.ToString("折算匯率")) row.CreateCell(25).SetCellValue(Convert.ToString("分類編號")) row.CreateCell(26).SetCellValue(Convert.ToString("分類名稱")) row.CreateCell(27).SetCellValue(Convert.ToString("單位單價")) row.CreateCell(28).SetCellValue(Convert.ToString("數量")) For k = 0 To ds1.Tables(0).Rows.Count - 1 Dim typeCount As Integer = ds1.Tables(0).Rows.Count iscost = ds1.Tables(0).Rows(k).Item("是否計算Cost單價").ToString row.CreateCell(29 + k).SetCellValue(ds1.Tables(0).Rows(k).Item("摘要").ToString()) 'oCells(1 + i + typeGroupNum, 18 + k) = ds1.Tables(0).Rows(k).Item("摘要").ToString() Next '參數類別分組區分 '循環關聯數據 For j = 0 To ds.Tables(0).Rows.Count - 1 If ds.Tables(0).Rows(j).Item("分類編號").ToString() = typeId Then Dim partId As String = ds.Tables(0).Rows(j).Item("料號").ToString() Dim companyNo As String = ds.Tables(0).Rows(j).Item("公司編號").ToString() Dim strPart As String = "select 參數,摘要 from MITDEVIL.料號_參數類別 where 分類編號='" + typeId + "' and 料號='" + partId + "' and 公司編號 = '" + companyNo + "' order by CAST(項目 AS INT )" Dim ds2 As DataSet = db.FillDataSet(strPart) '拼接前半部分數據 row = sheet.CreateRow(i + typeGroupNum + 1) row.CreateCell(0).SetCellValue(ds.Tables(0).Rows(j).Item("兄弟料號").ToString()) row.CreateCell(1).SetCellValue(ds.Tables(0).Rows(j).Item("公司編號").ToString()) row.CreateCell(2).SetCellValue(ds.Tables(0).Rows(j).Item("公司名稱").ToString()) row.CreateCell(3).SetCellValue(ds.Tables(0).Rows(j).Item("料號").ToString()) row.CreateCell(4).SetCellValue(ds.Tables(0).Rows(j).Item("階次1").ToString()) row.CreateCell(5).SetCellValue(ds.Tables(0).Rows(j).Item("階次2").ToString()) row.CreateCell(6).SetCellValue(ds.Tables(0).Rows(j).Item("階次3").ToString()) row.CreateCell(7).SetCellValue(ds.Tables(0).Rows(j).Item("階次4").ToString()) row.CreateCell(8).SetCellValue(ds.Tables(0).Rows(j).Item("階次5").ToString()) row.CreateCell(9).SetCellValue(ds.Tables(0).Rows(j).Item("階次6").ToString()) row.CreateCell(10).SetCellValue(ds.Tables(0).Rows(j).Item("階次7").ToString()) row.CreateCell(11).SetCellValue(ds.Tables(0).Rows(j).Item("階次8").ToString()) row.CreateCell(12).SetCellValue(ds.Tables(0).Rows(j).Item("階次9").ToString()) row.CreateCell(13).SetCellValue(ds.Tables(0).Rows(j).Item("階次10").ToString()) row.CreateCell(14).SetCellValue(ds.Tables(0).Rows(j).Item("類別編號").ToString()) row.CreateCell(15).SetCellValue(ds.Tables(0).Rows(j).Item("類別名稱").ToString()) row.CreateCell(16).SetCellValue(ds.Tables(0).Rows(j).Item("品名規格").ToString()) 'row.CreateCell(17).SetCellValue(ds.Tables(0).Rows(j).Item("單位").ToString()) row.CreateCell(18).SetCellValue(ds.Tables(0).Rows(j).Item("幣別").ToString()) row.CreateCell(19).SetCellValue(ds.Tables(0).Rows(j).Item("價格").ToString()) row.CreateCell(20).SetCellValue(ds.Tables(0).Rows(j).Item("Currency").ToString()) row.CreateCell(21).SetCellValue(ds.Tables(0).Rows(j).Item("Price_Book").ToString()) row.CreateCell(22).SetCellValue(ds.Tables(0).Rows(j).Item("基準幣別").ToString()) row.CreateCell(23).SetCellValue(ds.Tables(0).Rows(j).Item("基準單價").ToString()) row.CreateCell(24).SetCellValue(ds.Tables(0).Rows(j).Item("折算匯率").ToString()) row.CreateCell(25).SetCellValue(ds.Tables(0).Rows(j).Item("分類編號").ToString()) row.CreateCell(26).SetCellValue(ds.Tables(0).Rows(j).Item("分類名稱").ToString()) 'row.CreateCell(26).SetCellValue(ds.Tables(0).Rows(j).Item("實際價格").ToString()) row.CreateCell(28).SetCellValue(ds.Tables(0).Rows(j).Item("數量").ToString()) ''匯率計算 'SQLSTR = "SELECT " 'SQLSTR += "(SELECT 匯率 FROM [MITDEVIL].[匯率] A WHERE A.幣別=CASE WHEN '" + ds.Tables(0).Rows(j).Item("Currency").ToString() + "'='RMB' THEN 'CNY' ELSE '" + ds.Tables(0).Rows(j).Item("Currency").ToString() + "' END " 'SQLSTR += "AND NOT EXISTS (SELECT 1 FROM [MITDEVIL].[匯率] B WHERE A.幣別=B.幣別 AND B.匯率日期>A.匯率日期)) " 'SQLSTR += "/ " 'SQLSTR += "(SELECT 匯率 FROM [MITDEVIL].[匯率] A WHERE A.幣別=CASE WHEN '" + ds.Tables(0).Rows(j).Item("基準幣別").ToString() + "'='RMB' THEN 'CNY' ELSE '" + ds.Tables(0).Rows(j).Item("基準幣別").ToString() + "' END " 'SQLSTR += "AND NOT EXISTS (SELECT 1 FROM [MITDEVIL].[匯率] B WHERE A.幣別=B.幣別 AND B.匯率日期>A.匯率日期)) " 'row.CreateCell(24).SetCellValue(db.FillDataSet(SQLSTR).Tables(0).Rows(0)(0).ToString) '單位單價 --20220705,將EXP(SUM(LOG(參數)))替換為SUM(CAST(參數 AS FLOAT)) 先log 在exp 等於自身,沒有任何實際意義 '且計算過程中容易出現float溢出 cast預防出現 divide by zero 2.添加排除參數為NA的 AND A.參數 <>'NA' '202506130查询数据重复导致單價計算錯誤 SQLSTR = "SELECT '" + ds.Tables(0).Rows(j).Item("基準單價").ToString() + "'/ISNULL((CASE WHEN SUM(CAST(參數 AS FLOAT)) = 0 THEN 1 ELSE SUM(CAST(參數 AS FLOAT)) END),1) FROM ( " SQLSTR += "SELECT DISTINCT A.* FROM [MITDEVIL].[料號_階次類別] B, " SQLSTR += "[MITDEVIL].[料號_參數類別] A,[MITDEVIL].[料號_參數] C " SQLSTR += "WHERE A.兄弟料號 ='" + ds.Tables(0).Rows(j).Item("兄弟料號").ToString() + "' AND A.料號='" + ds.Tables(0).Rows(j).Item("料號").ToString() + "' AND A.公司編號 ='" + ds.Tables(0).Rows(j).Item("公司編號").ToString() + "' AND A.料號=B.料號 AND A.兄弟料號=B.兄弟料號 AND A.公司編號 = B.公司編號 AND A.分類編號=B.FMatClass AND A.分類名稱=B.FMatClassName AND B.FLAG='Y' " SQLSTR += "AND A.摘要=C.摘要 AND C.是否計算Cost單價='Y' AND A.參數<>'' AND A.參數 IS NOT NULL AND A.參數 <>'NA') A " row.CreateCell(27).SetCellValue(db.FillDataSet(SQLSTR).Tables(0).Rows(0)(0).ToString) For k = 0 To ds1.Tables(0).Rows.Count - 1 Dim typeCount As Integer = ds1.Tables(0).Rows.Count iscost = ds1.Tables(0).Rows(k).Item("是否計算Cost單價").ToString row.CreateCell(29 + k).SetCellValue(ds1.Tables(0).Rows(k).Item("摘要").ToString()) 'oCells(1 + i + typeGroupNum, 18 + k) = ds1.Tables(0).Rows(k).Item("摘要").ToString() Next For m = 0 To ds2.Tables(0).Rows.Count - 1 '拼接參數 row.CreateCell(29 + m).SetCellValue(ds2.Tables(0).Rows(m).Item("參數").ToString()) Next typeGroupNum = typeGroupNum + 1 End If Next Next Return workbook End Function Private Sub SaveDate(ByVal workbook As NPOI.SS.UserModel.IWorkbook, ByVal ExcelName As String) Dim ms As IO.MemoryStream Try '建立資料流 ms = New IO.MemoryStream() '寫入資料流 workbook.Write(ms) '在頁面輸出資料 Dim filename As String = "attachment;filename=" & HttpUtility.UrlEncode(ExcelName, Encoding.UTF8).ToString() Response.AddHeader("Content-Disposition", filename) Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312") Response.BinaryWrite(ms.ToArray()) Catch ex As Exception Finally '釋放資源 If Not ms Is Nothing Then ms.Dispose() ms.Close() End If End Try End Sub修改这段代码,新增导出过程中进度条显示
最新发布
06-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值