* IWebContext: Add Request body reading methods and properties.
* IWebContext: Add support for HttpOnly, and Secure cookies.
* Use file-scoped namespaces.
This commit is contained in:
2025-07-28 16:11:54 +02:00
parent 6dc19e8bbd
commit 9a480f5528
5 changed files with 110 additions and 49 deletions

View File

@@ -86,6 +86,27 @@ public class AspnetCoreWebContext : IWebContext
}
}
public string? RequestContentType => _context.Request.ContentType;
public long? RequestContentLength => _context.Request.ContentLength;
public byte[]? RequestReadBin()
{
if ((_context.Request.ContentLength ?? 0) == 0) { return null; }
byte[] content = new byte[_context.Request.ContentLength??0];
int pendingRead = (int) (_context.Request.ContentLength ?? 0);
int currentOffset = 0;
while (pendingRead > 0)
{
int readCount = _context.Request.Body.ReadAsync(content, currentOffset, pendingRead).GetAwaiter().GetResult();
currentOffset += readCount;
pendingRead -= readCount;
}
return content;
}
public void ResponseWrite(string text) { _context.Response.WriteAsync(text).GetAwaiter().GetResult(); }
public void ResponseWriteBin(byte[] content)
@@ -97,12 +118,18 @@ public class AspnetCoreWebContext : IWebContext
public void ResponseRedirect(string url) { _context.Response.Redirect(url); }
public void AddResponseCookie(string cookieName, string value, DateTime? expiration = null)
public void AddResponseCookie(
string cookieName,
string value,
DateTime? expiration = null,
bool httpOnly = false,
bool secure = false
)
{
_context.Response.Cookies.Append(
key: cookieName,
value: value,
options: new CookieOptions { Expires = expiration, }
options: new CookieOptions { Expires = expiration, HttpOnly = httpOnly, Secure = secure, }
);
}

View File

@@ -1,18 +1,17 @@
using Microsoft.AspNetCore.Http;
namespace VAR.WebFormsCore.AspNetCore.Code
namespace VAR.WebFormsCore.AspNetCore.Code;
public static class ExtensionMethods
{
public static class ExtensionMethods
#region IHeaderDictionary
public static void SafeSet(this IHeaderDictionary header, string key, string value) { header[key] = value; }
public static void SafeDel(this IHeaderDictionary header, string key)
{
#region IHeaderDictionary
public static void SafeSet(this IHeaderDictionary header, string key, string value) { header[key] = value; }
public static void SafeDel(this IHeaderDictionary header, string key)
{
if (header.ContainsKey(key)) { header.Remove(key); }
}
#endregion IHeaderDictionary
if (header.ContainsKey(key)) { header.Remove(key); }
}
#endregion IHeaderDictionary
}