C# IRequiresSessionState - 如何使用它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1375627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IRequiresSessionState - how do I use it?
提问by Praesagus
I need to be able to change when I can see session state. I found out about the IRequiresSessionState
Marker Interface, but have not been able to figure out how to use it. I think I may be missing something obvious. Can one of you C# guru's give me a quick 1-2-3 step through (e.g)?
当我可以看到会话状态时,我需要能够进行更改。我发现了IRequiresSessionState
标记界面,但一直无法弄清楚如何使用它。我想我可能遗漏了一些明显的东西。你们中的一位 C# 大师能给我一个快速的 1-2-3 步骤(例如)吗?
- Create a new class,
- Put
public interface IRequiresSessionState
in it. - Use
IRequiresSessionState('abra_cadabra')
to change the value.
- 创建一个新类,
- 放进
public interface IRequiresSessionState
去。 - 使用
IRequiresSessionState('abra_cadabra')
更改数值。
采纳答案by Praesagus
- To mark a class add a colon to the existing class name and put the marker.
If I had a class:
public class PageTools
Marked, it would look likepublic class PageTools : IRequiresSessionState
or with both interfaces,public class PageTools : IRequiresSessionState, IHttpHandler
. In my case, my class needed only to be marked with the first. My handler needed both. - If you right click on the marker you just typed in, you can choose
implement
from the menu (visual studio) and the necessary methods will be added to your class. Or you can look them up and add them manually. Once you have the IRequiresSessionState marker you can test to see if the session state is readonly and if so set a new http handler.
if (context.Handler is IReadOnlySessionState || context.Handler is IRequiresSessionState) { context.Handler = Handler(); }
The http handler: MSDNwill tell you a lot about HttpHandlers and HttpModules. In my case I needed a dummy handler so that I could access the session state when it was normally read only (
Page_PreInit
). So in my class I added this:protected IHttpHandler Handler() { MyHttpHandler resourceHttpHandler = HttpContext.Current.Handler as MyHttpHandler; if (resourceHttpHandler != null) // set the original handler back { return resourceHttpHandler.OriginalHandler; } // at this point session state should be available return HttpContext.Current.Handler; } public class MyHttpHandler : IHttpHandler, IRequiresSessionState { internal readonly IHttpHandler OriginalHandler; public MyHttpHandler(IHttpHandler originalHandler) { OriginalHandler = originalHandler; } public void ProcessRequest(HttpContext context) { // do not worry, ProcessRequest() will not be called, // but let's be safe throw new InvalidOperationException( "MyHttpHandler cannot process requests."); } public bool IsReusable { // IsReusable must be set to false since class has a member! get { return false; } } }
- 要标记一个类,在现有的类名中添加一个冒号并放置标记。如果我有一个类:
public class PageTools
标记,它看起来像public class PageTools : IRequiresSessionState
或同时具有两个接口,public class PageTools : IRequiresSessionState, IHttpHandler
. 就我而言,我的班级只需要标记为第一个。我的处理程序需要两者。 - 如果您右键单击您刚刚输入的标记,您可以
implement
从菜单(visual studio)中进行选择,必要的方法将添加到您的类中。或者您可以查找它们并手动添加它们。 拥有 IRequiresSessionState 标记后,您可以测试会话状态是否为只读,如果是,则设置新的 http 处理程序。
if (context.Handler is IReadOnlySessionState || context.Handler is IRequiresSessionState) { context.Handler = Handler(); }
http 处理程序:MSDN会告诉您很多有关 HttpHandlers 和 HttpModules 的信息。就我而言,我需要一个虚拟处理程序,以便在会话状态通常为只读 (
Page_PreInit
)时访问它。所以在我的课上我添加了这个:protected IHttpHandler Handler() { MyHttpHandler resourceHttpHandler = HttpContext.Current.Handler as MyHttpHandler; if (resourceHttpHandler != null) // set the original handler back { return resourceHttpHandler.OriginalHandler; } // at this point session state should be available return HttpContext.Current.Handler; } public class MyHttpHandler : IHttpHandler, IRequiresSessionState { internal readonly IHttpHandler OriginalHandler; public MyHttpHandler(IHttpHandler originalHandler) { OriginalHandler = originalHandler; } public void ProcessRequest(HttpContext context) { // do not worry, ProcessRequest() will not be called, // but let's be safe throw new InvalidOperationException( "MyHttpHandler cannot process requests."); } public bool IsReusable { // IsReusable must be set to false since class has a member! get { return false; } } }
Hereis a reference to a very elegant HttpModule class from which I got much or what I used. I hope this helps someone.
这是对一个非常优雅的 HttpModule 类的引用,我从中得到了很多或我使用的东西。我希望这可以帮助别人。
回答by Yoann. B
You have just to derive your HTTP Handlerclass from IRequiresSessionStateto get SessionState access.
您只需从IRequiresSessionState派生您的HTTP Handler类即可获得 SessionState 访问权限。
public class MyHttpHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
var MyValue = context.Session["MyKey"] as String;
MyValue = "Hello World";
context.Session["MyKey"] = MyValue;
}
public bool IsReusable
{
get { return true; }
}
}
回答by mkchandler
The IRequiresSessionState
interface is a marker interface and contains no methods, so you can't use it the way you are asking in the question. You only implement the interface in an HTTP handler to identify that the handler requires read and write access to session-state values.
该IRequiresSessionState
接口是一个标记接口,不包含任何方法,因此您不能按照您在问题中提出的方式使用它。您仅在 HTTP 处理程序中实现该接口以标识处理程序需要对会话状态值进行读写访问。
To see how you implement it in an HTTP handler, check out this link: http://www.willasrari.com/blog/irequiressessionstate-and-dynamic-data-in-static-pages/000262.aspx
要了解如何在 HTTP 处理程序中实现它,请查看此链接:http: //www.willasrari.com/blog/irequiressessionstate-and-dynamic-data-in-static-pages/000262.aspx