This topic is locked

Write/read from cookie

6/21/2024 9:18:58 AM
ASPRunner.NET General questions
T
Tim author

Does anyone have an example of writing/reading to/from a cookie in ASPR.Net?

When I Google it it looks like, in ASP.Net C#, I need to use the HttpRequest class, but this is not available by default in ASPR. I believe I need to add an external C# file with custom functions. Pete gave a great example here, but I am struggling with adding what is needed for the cookie read/write. Any help would be appreciated.

Thanks,
Tim

Sergey Kornilov 6/24/2024

You do not need to do anything "custom" since HttpRequest is a part of .NET framework. Like I said, you have two options here.

  1. Either use a fully-qualified name of HttpRequest class so your code compiles properly.
  2. Or use C# imports event in ASPRunner.NET to include additiona modules/namespaces so the compiler knows what HttpRequest is.

More info:
https://xlinesoft.com/asprunnernet/docs/c-imports.htm
https://learn.microsoft.com/en-us/dotnet/api/system.web.httprequest?view=netframework-4.8.1

E
Eric Chan 7/26/2024

Hi Tim,

FYR. ASPR.Net contains some built-in cookie functions to writing/reading the cookies. You may try the following C# code snippet.

XVar cookieValue = "anythinig";
MVCFunctions.SetCookie("CookieName", cookieValue, null, appName, null, null, null, true); // save a session cookie and restricted the accessing by appName only
cookieValue = MVCFunctions.GetCookie("CookieName");
MVCFunctions.RemoveCookie("CookieName");

Cheers,
Eric