Developer forum

Forum » Development » Setting cookie trough querystring parameter

Setting cookie trough querystring parameter

Rob Lohmann
Reply

For one of our websites we needs to be able to set a cookie to influence some of the behavior on the website. We currently do this using a cookie which is set when a certain value is in the query string.

For example;

http://www.domain.com/default.aspx?m=1

The following piece of code in the global.asax file handles this request;

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires at the beginning of each request
        Dynamicweb.Frontend.GlobalAsaxHandler.Application_BeginRequest(sender, e)

        If HttpContext.Current.Request.QueryString("m") IsNot Nothing Then
            Dim modeValue As String = HttpContext.Current.Request.QueryString("m")
            If modeValue.Equals("1", StringComparison.OrdinalIgnoreCase) AndAlso Not Helper.HasValidCookie("mode") Then
                Helper.SetCookie("mode", "1", DateTime.Now.AddYears(1))
            End If
        End If
    End Sub
 

Whenever we run this an error is thrown (see attachement). DW seems to be looking for an item type, but this one isn't available.
What could possibly be causing this issue? Is it something in the code? Or doesn't DW accept querystrings?

 

 

kiosk_error.PNG

Replies

 
Nicolai Høeg Pedersen
Reply

Hi Rob

 

It is the parameter m in the querystring that triggers some Things in our modules.

So change the M= to i.e. c=...

 

BR Nicolai

 
Rob Lohmann
Reply

Hi Nicolai,

Thank you for the quick response. Unfortunately this doesn't seem to work :-( I'm now using 'c' as querystring parameter, but this throws the same error. So caling http://www.domain.com/?c=1 would result in the same error. The code with this is;

 

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires at the beginning of each request
    Dynamicweb.Frontend.GlobalAsaxHandler.Application_BeginRequest(sender, e)
 
    If HttpContext.Current.Request.QueryString("c") IsNot Nothing Then
        Dim modeValue As String = HttpContext.Current.Request.QueryString("c")
        If modeValue.Equals("1", StringComparison.OrdinalIgnoreCase) AndAlso Not Helper.HasValidCookie("mode") Then
            Helper.SetCookie("mode", "1", DateTime.Now.AddYears(1))
        End If
    End If
End Sub

 


The DW version is 8.3.1.8 by the way. Do you have any other suggestions?


BR Rob

 

 

 

 

 

 

 

 
Nicolai Høeg Pedersen
Reply

Hi Rob

 

Sorry - do not think I read your stack close enough.

 

Cannot see the beginning of your stack from the screendump. But I can see that is is Default.aspx that causes the error, and if it was the global.asax code you would not have that stack?

 

If you do not add the c= or m= to the querystring, will the same page then work? I cannot see how that would be possible...

 

What is Helper? And does it call somewhere to DW?

 

And it seems like you have an Item type definition called http, but it does not exist in database. Make sure that the SQL user have DDL rights on the database.

 

Hop this can give you some hints to find the error.

 

BR Nicolai

 
Rob Lohmann
Reply

Hi Nicolai,

I'll post you the full stack trace in the end of this response. It's partially correct that default.aspx casues the error.. When debugging I see that the error is caused in the 'Application_Error' sub of the global.asax. This contains the following code;

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires when an error occurs
        Dynamicweb.Frontend.GlobalAsaxHandler.Application_Error(sender, e)
    End Sub

Since the error isn't catched here the execution of the code continues untill it reaches the default.aspx. There the 'new PageviewControl'-function catches and throws the error. So the actual error is thrown here..

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Output.Controls.Add(New PageviewControl())
    End Sub

The page continues to work without the querystring parameter in it, but as soon as I add it (regardless of the variation used) it throws this particular error. All of the query strings below where tested and are throwing this error;

- c=1
- m=1
- m=kiosk
- mode=kiosk

Helper is a C# class with a set of methods which does nothing more then setting a cookie to the current response;

public static void SetCookie(string key, string value, DateTime expires)
        {
            try
            {
                var cookie = new HttpCookie(key, value) {Expires = expires};
                HttpContext.Current.Response.Cookies.Add(cookie);
                HttpContext.Current.Request.Cookies.Add(cookie);
            }
            catch (Exception err)
            {
                Logger.ErrorException("An error occured while setting cookie cookie with the key: " + key + " and value: " + value, err);
            }

        }

The strange thing also is that when debbuging we can get passed the SetCookie method, so this definately isn't causing the problem..

 

 

The first thing that occured to us as well is that there is/was an item type called 'http'. But this definately isn't the issue because there isn't an item type with this name available in the solution. I've tried adding a table to the database with the name 'ItemType_Http' and this caused the error to change, so it definately seems to be looking for an ItemType somehow...But it only does this when the QS-parameter is added and it is a non-existing item type..

 

 

We also ruled out the database user, it's DB owner at the moment. Do you have any other suggestions? Perhaps you can check and test the solution? I've shipped it over to your support regarding another issue mentioned in case CAS-101837-7GWDY4

 
Nicolai Høeg Pedersen
Reply

Hi Rob

 

Have you chekked that you even have a response at the time the Application_BeginRequest event is fired...?

 

BR Nicolai

 
Nicolai Høeg Pedersen
 
Rob Lohmann
Reply

Hi Nicolai,

I would have expected an error because the cookie helper function explicitly sets the cookie for the response, but i've checked this and we indeed do have a context.response and context.request object available when the event is fired.

I've also taken a look at the thread you mentioned and added the web.config value which is mentioned there, but unfortunately this doesn't do anything..

I forgotten the full stack trace last time, but here it is;

Stack Trace:


[SqlException (0x80131904): Invalid object name 'ItemType_http'.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1767866
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5352418
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1691
   System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +61
   System.Data.SqlClient.SqlDataReader.get_MetaData() +90
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +365
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) +1406
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +177
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +134
   System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41
   System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +10
   Dynamicweb.Database.CreateDataReader(String sql, String database, IDbConnection connection, IDbTransaction transaction, Int32 commandTimeout) +586
   Dynamicweb.Content.Items.Queries.Repository.SelectByIds(IEnumerable`1 ids, Query query) +833
   Dynamicweb.Content.Items.Queries.Repository.SelectById(String id, Query query) +51
   Dynamicweb.Content.Items.Queries.Repository.SelectById(String id) +12
   Dynamicweb.Content.Items.Queries.StorageManager.GetById(String systemName, String id) +53
   Dynamicweb.Content.Items.Rendering.Renderer.RenderValueInternal(Object value, Type valueType, Template template, String tagName, ItemField meta, Boolean tryRenderLinkedItem, ItemEntry itemEntry) +651
   Dynamicweb.Content.Items.Rendering.Renderer.RenderItem(ItemEntry item, ItemSettings settings) +1435
   Dynamicweb.Frontend.Content.WriteParagraph(ParagraphTemplateJoinClass PtRecord) +3767
   Dynamicweb.Frontend.OutputReplacer.InsertGlobalParagraphs(String html, Content Content) +1798
   Dynamicweb.Frontend.PageView.Output() +1772
   Dynamicweb.Frontend.PageviewControl.ParseControls() +56
   Dynamicweb.Frontend.PageviewControl.OnInit(EventArgs e) +86
   System.Web.UI.Control.InitRecursive(Control namingContainer) +134
   System.Web.UI.Control.AddedControl(Control control, Int32 index) +191
   System.Web.UI.ControlCollection.Add(Control child) +86
   Overvecht.Web._Default.Page_Load(Object sender, EventArgs e) in C:\Projects\Dynamicweb\Groenblok BV\Kiosk\Application\Default.aspx.vb:6
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

 

 

 

 

 

 

 
Nicolai Høeg Pedersen
Reply

Hi Rob

 

My point exactly. You listen to the event Begin Request which is fired by ASP.NET so early in the execution process that there is no request or response available.

 

So you cannot do it that early. You need to move your code to another of global.asax events. Any reason you need to set it that early in the process?

 

BR Nicolai

 

You must be logged in to post in the forum