Add cookie support on IWebContext.

This commit is contained in:
2023-05-28 05:09:18 +02:00
parent 1eb0fea182
commit 2c519bf122
2 changed files with 37 additions and 2 deletions

View File

@@ -36,6 +36,23 @@ public class AspnetCoreWebContext : IWebContext
} }
} }
private Dictionary<string, string>? _requestCookies;
public Dictionary<string, string> RequestCookies
{
get
{
if (_requestCookies == null)
{
_requestCookies = _context.Request.Cookies
.ToDictionary(p => p.Key, p => p.Value);
}
return _requestCookies;
}
}
private Dictionary<string, string?>? _requestQuery; private Dictionary<string, string?>? _requestQuery;
public Dictionary<string, string?> RequestQuery public Dictionary<string, string?> RequestQuery
@@ -95,6 +112,20 @@ public class AspnetCoreWebContext : IWebContext
_context.Response.Redirect(url); _context.Response.Redirect(url);
} }
public void AddResponseCookie(string cookieName, string value, DateTime? expiration = null)
{
_context.Response.Cookies.Append(
key: cookieName,
value: value,
options: new CookieOptions {Expires = expiration,}
);
}
public void DelResponseCookie(string cookieName)
{
_context.Response.Cookies.Delete(cookieName);
}
public bool ResponseHasStarted => _context.Response.HasStarted; public bool ResponseHasStarted => _context.Response.HasStarted;
public int ResponseStatusCode public int ResponseStatusCode

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace VAR.WebFormsCore.Code; namespace VAR.WebFormsCore.Code;
@@ -6,14 +7,17 @@ public interface IWebContext
{ {
string RequestPath { get; } string RequestPath { get; }
string RequestMethod { get; } string RequestMethod { get; }
Dictionary<string, string?> RequestForm { get; }
Dictionary<string, string?> RequestQuery { get; }
Dictionary<string, string?> RequestHeader { get; } Dictionary<string, string?> RequestHeader { get; }
Dictionary<string, string> RequestCookies { get; }
Dictionary<string, string?> RequestQuery { get; }
Dictionary<string, string?> RequestForm { get; }
void ResponseWrite(string text); void ResponseWrite(string text);
void ResponseWriteBin(byte[] content); void ResponseWriteBin(byte[] content);
void ResponseFlush(); void ResponseFlush();
void ResponseRedirect(string url); void ResponseRedirect(string url);
void AddResponseCookie(string cookieName, string value, DateTime? expiration = null);
void DelResponseCookie(string cookieName);
bool ResponseHasStarted { get; } bool ResponseHasStarted { get; }
int ResponseStatusCode { get; set; } int ResponseStatusCode { get; set; }