Sil*_*ght 1 c# asp.net session httpcontext ihttphandler
我有一个类似于此线程中描述的情况:
如何在c#中的静态方法中获取会话变量的值?
但是,这里没有静态方法(只是一个继承自IHttpHandler的类)
这是我的代码:
<%@ WebHandler Language="C#" Class="Telerik.Web.Examples.FileExplorer.FilterAndDownloadFiles.Handler" %>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Text;
using Telerik.Web.UI;
namespace Telerik.Web.Examples.FileExplorer.FilterAndDownloadFiles
{
[RadCompressionSettings(HttpCompression = CompressionType.None)] // Disable RadCompression for this page ;
public class Handler : IHttpHandler
{
#region IHttpHandler Members
private HttpContext _context;
private HttpContext Context
{
get
{
return _context;
}
set
{
_context = value;
}
}
public void ProcessRequest(HttpContext context)
{
Context = context;
string filePath = context.Request.QueryString["path"];
filePath = context.Server.MapPath(filePath);
if (filePath == null)
{
return;
}
System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath);
System.IO.BinaryReader br = new System.IO.BinaryReader(streamReader.BaseStream);
byte[] bytes = new byte[streamReader.BaseStream.Length];
br.Read(bytes, 0, (int)streamReader.BaseStream.Length);
if (bytes == null)
{
return;
}
streamReader.Close();
br.Close();
string fileName = System.IO.Path.GetFileName(filePath);
string MimeType = GetMimeType(fileName);
string extension = System.IO.Path.GetExtension(filePath);
char[] extension_ar = extension.ToCharArray();
string extension_Without_dot = string.Empty;
for (int i = 1; i < extension_ar.Length; i++)
{
extension_Without_dot += extension_ar[i];
}
//if (extension == ".jpg")
//{ // Handle *.jpg and
// WriteFile(bytes, fileName, "image/jpeg jpeg jpg jpe", context.Response);
//}
//else if (extension == ".gif")
//{// Handle *.gif
// WriteFile(bytes, fileName, "image/gif gif", context.Response);
//}
if (HttpContext.Current.Session["User_ID"] != null)
{
WriteFile(bytes, fileName, MimeType + " " + extension_Without_dot, context.Response);
}
}
/// <summary>
/// Sends a byte array to the client
/// </summary>
/// <param name="content">binary file content</param>
/// <param name="fileName">the filename to be sent to the client</param>
/// <param name="contentType">the file content type</param>
private void WriteFile(byte[] content, string fileName, string contentType, HttpResponse response)
{
response.Buffer = true;
response.Clear();
response.ContentType = contentType;
response.AddHeader("content-disposition", "attachment; filename=" + fileName);
response.BinaryWrite(content);
response.Flush();
response.End();
}
private string GetMimeType(string fileName)
{
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
}
public bool IsReusable
{
get
{
return false;
}
}
#endregion
}
}
Run Code Online (Sandbox Code Playgroud)
if (HttpContext.Current.Session["User_ID"] != null)当session变量为null时, 我得到以下异常:
'/'应用程序中的服务器错误.
你调用的对象是空的.描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例
我怎样才能解决这个问题?
由于通用处理程序被设计为准系统请求处理器,因此它们默认不支持会话.你需要做的是用IRequiresSessionState接口装饰你的处理程序类:
public class Handler : IHttpHandler, IRequiresSessionState
Run Code Online (Sandbox Code Playgroud)
这会导致ASP.NET运行时初始化请求的会话容器.然后,您可以使用您的Session实例HttpContext.
| 归档时间: |
|
| 查看次数: |
2015 次 |
| 最近记录: |