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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:49:37  来源:igfitidea点击:

IRequiresSessionState - how do I use it?

c#asp.net

提问by Praesagus

I need to be able to change when I can see session state. I found out about the IRequiresSessionStateMarker 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 步骤(例如)吗?

  1. Create a new class,
  2. Put public interface IRequiresSessionStatein it.
  3. Use IRequiresSessionState('abra_cadabra')to change the value.
  1. 创建一个新类,
  2. 放进public interface IRequiresSessionState去。
  3. 使用IRequiresSessionState('abra_cadabra')更改数值。

采纳答案by Praesagus

  1. To mark a class add a colon to the existing class name and put the marker. If I had a class: public class PageToolsMarked, it would look like public class PageTools : IRequiresSessionStateor 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.
  2. If you right click on the marker you just typed in, you can choose implementfrom the menu (visual studio) and the necessary methods will be added to your class. Or you can look them up and add them manually.
  3. 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();
    }
    
  4. 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; }
        }
    }
    
  1. 要标记一个类,在现有的类名中添加一个冒号并放置标记。如果我有一个类:public class PageTools标记,它看起来像public class PageTools : IRequiresSessionState或同时具有两个接口,public class PageTools : IRequiresSessionState, IHttpHandler. 就我而言,我的班级只需要标记为第一个。我的处理程序需要两者。
  2. 如果您右键单击您刚刚输入的标记,您可以implement从菜单(visual studio)中进行选择,必要的方法将添加到您的类中。或者您可以查找它们并手动添加它们。
  3. 拥有 IRequiresSessionState 标记后,您可以测试会话状态是否为只读,如果是,则设置新的 http 处理程序。

    if (context.Handler is IReadOnlySessionState
        || context.Handler is IRequiresSessionState)
    {
        context.Handler = Handler();
    }
    
  4. 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 IRequiresSessionStateinterface 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