locked
Where do I place EnableSessionState = "ReadOnly" directive in Webmatrix? RRS feed

  • Question

  • User-687931224 posted

    I am trying to use session variables in Webmatrix with the "ReadOnly" setting on most of the pages to prevent unnecessary locking.  I did a forum search using EnableSessionState as the keyword can got no results.

    So where do I place this directive such that most of the pages use Read Only session and only 1 page sets the sets the variables.

    Is there new syntax to accomplish this?

    thanks

    Friday, October 25, 2013 11:03 AM

Answers

  • User-821857111 posted

    There is no declarative syntax in Web Pages like in Web Forms. You will have to implement an HttpModule to do this because the session state behaviour needs to be set early in the request processing pipeline. You need a C# class file which needs to go into an App_Code folder:

    using System;
    using System.Web;
    
    /// <summary>
    /// Summary description for SessionStateModule
    /// </summary>
    public class SessionStateModule : IHttpModule
    {
        public void Dispose(){
    
        }
    
        public void Init(HttpApplication context){
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
    
        void context_BeginRequest(object sender, EventArgs e){
        
            HttpContext currentContext = (sender as HttpApplication).Context;
            if (!currentContext.Request.Url.ToString().Contains("Some value")){
                currentContext.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.ReadOnly);
            }
            else {
                currentContext.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Default);
            }
    
        }
    }

    The above example checks the URL at the start of the request, and if it contains "some value", the session state behavior is set to Default, otherwise it is set to ReadOnly.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 25, 2013 3:00 PM
  • User-821857111 posted

    You don't need to rename any pages. You just change the method to suit your needs:

    Imports System.Web
    
    ''' <summary>
    ''' Summary description for SessionStateModule
    ''' </summary>
    Public Class SessionStateModule
    	Implements IHttpModule
    	Public Sub Dispose()
    
    	End Sub
    
    	Public Sub Init(context As HttpApplication)
    		context.BeginRequest += New EventHandler(AddressOf context_BeginRequest)
    	End Sub
    
    	Private Sub context_BeginRequest(sender As Object, e As EventArgs)
    
    		Dim currentContext As HttpContext = TryCast(sender, HttpApplication).Context
    		If currentContext.Request.Url.ToString().Contains("FileList") Then
    			currentContext.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.ReadOnly)
    		Else
    			currentContext.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Default)
    		End If
    
    	End Sub
    End Class

    I've used this to convert from C# to VB: http://converter.telerik.com/. Hopefully it works.

    You can mix .cshtml and.vbhtml files in the same site, but you cannot mix .cs and .vb files in App_Code. They have to be one langauge or the other. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 25, 2013 3:43 PM

All replies

  • User-2005691517 posted

    It is set in the @Page directive

    <% @Page EnableSessionState="ReadOnly" %>

    It can also be set at a global level in the configuration file

    http://msdn.microsoft.com/en-us/library/950xf363(v=vs.85).aspx

    Friday, October 25, 2013 11:12 AM
  • User-687931224 posted

    Yes that is the syntax I use in Webforms. But I am using Razor syntax in Webmatrix and can't seem to place that code correctly into the content pages.

    When I try to place it at the top of a content page in the initial code block like this...

    @Code
        @Page EnableSessionState="ReadOnly"
        WebSecurity.RequireAuthenticatedUser()

    End Code

    An error is thrown stating "EnableSessionState is not declared".

    That is why I ask.

    Thank You

    Friday, October 25, 2013 11:49 AM
  • User-821857111 posted

    There is no declarative syntax in Web Pages like in Web Forms. You will have to implement an HttpModule to do this because the session state behaviour needs to be set early in the request processing pipeline. You need a C# class file which needs to go into an App_Code folder:

    using System;
    using System.Web;
    
    /// <summary>
    /// Summary description for SessionStateModule
    /// </summary>
    public class SessionStateModule : IHttpModule
    {
        public void Dispose(){
    
        }
    
        public void Init(HttpApplication context){
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
    
        void context_BeginRequest(object sender, EventArgs e){
        
            HttpContext currentContext = (sender as HttpApplication).Context;
            if (!currentContext.Request.Url.ToString().Contains("Some value")){
                currentContext.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.ReadOnly);
            }
            else {
                currentContext.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Default);
            }
    
        }
    }

    The above example checks the URL at the start of the request, and if it contains "some value", the session state behavior is set to Default, otherwise it is set to ReadOnly.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 25, 2013 3:00 PM
  • User-687931224 posted

    Mike,

    Thanks so much. Wow..all that to replace a simple declarative statement!

    I presume then that I must name my pages to contain the "some value" and that the "some value" must be string that is not likely to be found in most URLs.

    So for example one of the read-only session pages I want to use is currently called FileList or http://domain.com/Filelist.

    If I decide to use "Redo" as the "some value" then I must name the page FileListRedo for the session to be read-only on that page?

    Can I mix C# code with VB? Please don't flame me you VB detractors.

    Thanks again

    Friday, October 25, 2013 3:30 PM
  • User-821857111 posted

    You don't need to rename any pages. You just change the method to suit your needs:

    Imports System.Web
    
    ''' <summary>
    ''' Summary description for SessionStateModule
    ''' </summary>
    Public Class SessionStateModule
    	Implements IHttpModule
    	Public Sub Dispose()
    
    	End Sub
    
    	Public Sub Init(context As HttpApplication)
    		context.BeginRequest += New EventHandler(AddressOf context_BeginRequest)
    	End Sub
    
    	Private Sub context_BeginRequest(sender As Object, e As EventArgs)
    
    		Dim currentContext As HttpContext = TryCast(sender, HttpApplication).Context
    		If currentContext.Request.Url.ToString().Contains("FileList") Then
    			currentContext.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.ReadOnly)
    		Else
    			currentContext.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Default)
    		End If
    
    	End Sub
    End Class

    I've used this to convert from C# to VB: http://converter.telerik.com/. Hopefully it works.

    You can mix .cshtml and.vbhtml files in the same site, but you cannot mix .cs and .vb files in App_Code. They have to be one langauge or the other. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 25, 2013 3:43 PM
  • User-687931224 posted

    Thank you Mike,

    As always your input is invaluable.

    I will use that Class file and then use a couple lines of code to test the SessionStateBehavior in a particular page.

    Would this code properly test the SessionStateBehaviorof a particular page?

    'Usage
    Dim requestContext As RequestContext 
    Dim returnValue As SessionStateBehavior 
    
    returnValue = Me.GetSessionStateBehavior(requestContext)

    Have a great day<>

    Tuesday, October 29, 2013 10:41 AM