IRequiresSessionState—如何使用它?

时间:2021-08-26 11:49:46

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标记接口,但是还没有找到如何使用它的方法。我想我可能漏掉了一些明显的东西。你们中有谁能给我一个快速的1-2-3步通过(例如)?

  1. Create a new class,
  2. 创建一个新的类,
  3. Put public interface IRequiresSessionState in it.
  4. 将公共接口IRequiresSessionState放在其中。
  5. Use IRequiresSessionState('abra_cadabra') to change the value.
  6. 使用IRequiresSessionState(“abra_cadabra”)来更改值。

3 个解决方案

#1


11  

  1. 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 like public 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.
  2. 要标记一个类,请向现有的类名添加一个冒号并放置标记。如果我有一个类:public class PageTools,它看起来就像public class PageTools: IRequiresSessionState或者同时有两个接口,public class PageTools: IRequiresSessionState, IHttpHandler。在我的例子中,我的类只需要用第一个标记。我处理程序需要两个。
  3. 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.
  4. 如果您右键单击您刚刚输入的标记,您可以从菜单(visual studio)中选择实现,必要的方法将被添加到您的类中。或者您可以查找并手动添加它们。
  5. 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.

    一旦拥有了IRequiresSessionState标记,您就可以测试会话状态是否为只读的,以及是否设置了新的http处理程序。

    if (context.Handler is IReadOnlySessionState
        || context.Handler is IRequiresSessionState)
    {
        context.Handler = Handler();
    }
    
  6. The http handler: MSDN will 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:

    http处理程序:MSDN将告诉您大量关于httphandler和httpmodule的信息。在我的例子中,我需要一个伪处理程序,以便在会话状态通常只被读取时访问它(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; }
        }
    }
    

Here is a reference to a very elegant HttpModule class from which I got much or what I used. I hope this helps someone.

下面是对一个非常优雅的HttpModule类的引用,我从这个类中获得了很多或者我使用了什么。我希望这能帮助某人。

#2


9  

You have just to derive your HTTP Handler class from IRequiresSessionState to get SessionState access.

您只需从IRequiresSessionState派生HTTP处理程序类来获得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; }
    }
}

#3


2  

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- dynamic-data- static-pages/000262.aspx

#1


11  

  1. 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 like public 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.
  2. 要标记一个类,请向现有的类名添加一个冒号并放置标记。如果我有一个类:public class PageTools,它看起来就像public class PageTools: IRequiresSessionState或者同时有两个接口,public class PageTools: IRequiresSessionState, IHttpHandler。在我的例子中,我的类只需要用第一个标记。我处理程序需要两个。
  3. 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.
  4. 如果您右键单击您刚刚输入的标记,您可以从菜单(visual studio)中选择实现,必要的方法将被添加到您的类中。或者您可以查找并手动添加它们。
  5. 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.

    一旦拥有了IRequiresSessionState标记,您就可以测试会话状态是否为只读的,以及是否设置了新的http处理程序。

    if (context.Handler is IReadOnlySessionState
        || context.Handler is IRequiresSessionState)
    {
        context.Handler = Handler();
    }
    
  6. The http handler: MSDN will 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:

    http处理程序:MSDN将告诉您大量关于httphandler和httpmodule的信息。在我的例子中,我需要一个伪处理程序,以便在会话状态通常只被读取时访问它(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; }
        }
    }
    

Here is a reference to a very elegant HttpModule class from which I got much or what I used. I hope this helps someone.

下面是对一个非常优雅的HttpModule类的引用,我从这个类中获得了很多或者我使用了什么。我希望这能帮助某人。

#2


9  

You have just to derive your HTTP Handler class from IRequiresSessionState to get SessionState access.

您只需从IRequiresSessionState派生HTTP处理程序类来获得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; }
    }
}

#3


2  

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- dynamic-data- static-pages/000262.aspx