Replacing Response.Add_Cookie Function

Description

Response.Add_Cookie() was marked as deprecated in the V10 release and has been removed in V11. Any code which attempts to use this method will generate an error and must be updated.

This article applies to developers who are migrating applications from Alpha Five V10 or older to a newer release of Alpha Anywhere.

Migrating to Alpha Anywhere

In Alpha Anywhere, cookies are added and accessed through the Context object. To read cookies, use the Context.Request.Cookies.Get function to get a cookie. You can call the Context.Request.HasCookie method to check if the cookie exists before fetching it. If you request a cookie that doesn't exist using Context.Request.Cookies.Get, the cookie will be created.

Cookies are added through the Context.Response object. Use the Context.Response.AppendCookie method to add a cookie.

For more information on adding and reading cookies in Alpha Anywhere, see How to Create and Read Cookies.

Migrating to Alpha Five Version 11

In Alpha Five Version 11, sending cookies at part of the Response is done using the Response.Cookie property.

Response.Cookies is a .NET System::Web::HttpCookieCollection, and it is functionally equivalent to ASP.NET's HttpResponse.Cookies property.

Adding a cookie to the Response.Cookies System::Web::HttpCookieCollection object is done using either the Add or Set method, both of which take a .NET System::Web::HttpCookie as their only argument.

If you are migrating to Alpha Anywhere, please follow the migration instructions in the section above, Migrating to Alpha Anywhere.
  • The Add method adds a cookie without checking to see if it already exists in the collection. The Add allows duplicate cookies in the cookie collection.

  • The Set method first checks to see if a cookie already exists in the collection and if so updates it. The Set method does not allow duplicate cookies in the cookie collection.

Because these methods take a System::Web::HttpCookie .NET object and add it to the Response.Cookies collection, the Xbasic programmer must create the .NET object and then add it.

'first create a new cookie
dim NewCookie as System::Web::HttpCookie = new System::Web::HttpCookie("Cookie1", "Cookie1's data")
'now add/set the cookie
Response.Cookies.Set(NewCookie)

When adding a simple cookie such as this, the code can be consolidated into a single line of code.

'add/set a simple cookie
Response.Cookies.Set(new System::Web::HttpCookie("Cookie1", "Cookie1"))

However, it is not possible to set any other properties of the cookie with this abbreviated syntax. If there is a need to set the path, lifetime, Secure flag, HttpOnly flag or any other property, the longer syntax is required.

'create the new cookie
dim NewCookie as System::Web::HttpCookie = new System::Web::HttpCookie("This is the data in Cookie1")
'set the value (data to be stored in the cookie
NewCookie.Value = "Cookie2"
. make this an "This is the data in my Cookie2" cookie
NewCookie.HttpOnly = .t.
'restrict the cookie to the "/MyApplication" path
NewCookie.Path = "/MyApplication"

'finally, add/set the cookie
Response.Cookies.Add(NewCookie)

See Also