Enabled nullable checks and fix nullability warnings.

This commit is contained in:
2023-05-22 03:25:56 +02:00
parent 9050b3796d
commit 2a474aac0a
27 changed files with 160 additions and 117 deletions

View File

@@ -1,4 +1,3 @@
using System;
using VAR.WebFormsCore.Code; using VAR.WebFormsCore.Code;
using VAR.WebFormsCore.Controls; using VAR.WebFormsCore.Controls;
using VAR.WebFormsCore.Pages; using VAR.WebFormsCore.Pages;
@@ -7,9 +6,9 @@ namespace VAR.WebFormsCore.TestWebApp;
public class FrmDefault: PageCommon public class FrmDefault: PageCommon
{ {
private CTextBox _txtText = new CTextBox { ID = "txtText", CssClassExtra = "width150px", AllowEmpty = false }; private readonly CTextBox _txtText = new() { ID = "txtText", CssClassExtra = "width150px", AllowEmpty = false };
private Button _btnTest = new Button { ID = "btnTest", }; private readonly Button _btnTest = new() { ID = "btnTest", };
private Label _lblMessage = new Label { ID = "lblMessage", }; private readonly Label _lblMessage = new() { ID = "lblMessage", };
public FrmDefault() public FrmDefault()
{ {
@@ -17,7 +16,7 @@ public class FrmDefault: PageCommon
Init += FrmLogin_Init; Init += FrmLogin_Init;
} }
private void FrmLogin_Init(object sender, EventArgs e) private void FrmLogin_Init(object? sender, EventArgs e)
{ {
InitializeControls(); InitializeControls();
} }

View File

@@ -1,6 +1,3 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace VAR.WebFormsCore.TestWebApp namespace VAR.WebFormsCore.TestWebApp
{ {
public class Program public class Program

View File

@@ -1,7 +1,4 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using VAR.WebFormsCore.Code; using VAR.WebFormsCore.Code;
namespace VAR.WebFormsCore.TestWebApp namespace VAR.WebFormsCore.TestWebApp

View File

@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ID/@EntryIndexedValue">ID</s:String></wpf:ResourceDictionary>

View File

@@ -11,11 +11,11 @@ namespace VAR.WebFormsCore.Code
{ {
#region Declarations #region Declarations
private readonly Assembly _assembly; private readonly Assembly? _assembly;
private readonly string _assemblyNamespace; private readonly string? _assemblyNamespace;
private List<string> _assemblyFiles; private List<string>? _assemblyFiles;
private readonly string _absolutePath; private readonly string? _absolutePath;
private List<string> _absoluteFiles; private List<string>? _absoluteFiles;
#endregion Declarations #endregion Declarations
@@ -62,7 +62,7 @@ namespace VAR.WebFormsCore.Code
#region Creator #region Creator
public Bundler(Assembly assembly = null, string assemblyNamespace = null, string absolutePath = null) public Bundler(Assembly? assembly = null, string? assemblyNamespace = null, string? absolutePath = null)
{ {
_assembly = assembly; _assembly = assembly;
_assemblyNamespace = assemblyNamespace; _assemblyNamespace = assemblyNamespace;
@@ -81,7 +81,7 @@ namespace VAR.WebFormsCore.Code
response.ContentType = contentType; response.ContentType = contentType;
foreach (string fileName in AssemblyFiles) foreach (string fileName in AssemblyFiles)
{ {
Stream resourceStream = _assembly.GetManifestResourceStream(fileName); Stream? resourceStream = _assembly?.GetManifestResourceStream(fileName);
if (resourceStream != null) if (resourceStream != null)
{ {
string fileContent = new StreamReader(resourceStream).ReadToEnd(); string fileContent = new StreamReader(resourceStream).ReadToEnd();

View File

@@ -9,7 +9,7 @@ namespace VAR.WebFormsCore.Code
{ {
#region HttpContext #region HttpContext
public static string GetRequestParm(this HttpContext context, string parm) public static string? GetRequestParm(this HttpContext context, string parm)
{ {
if (context.Request.Method == "POST") if (context.Request.Method == "POST")
{ {

View File

@@ -1,32 +1,61 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Http;
namespace VAR.WebFormsCore.Code namespace VAR.WebFormsCore.Code
{ {
public static class GlobalConfig public static class GlobalConfig
{ {
private static IGlobalConfig _globalConfig; private static IGlobalConfig? _globalConfig;
public static IGlobalConfig Get() public static IGlobalConfig Get()
{ {
if (_globalConfig != null) { return _globalConfig; } if (_globalConfig != null) { return _globalConfig; }
Type iGlobalConfig = typeof(IGlobalConfig); Type iGlobalConfig = typeof(IGlobalConfig);
Type foundGlobalConfig = AppDomain.CurrentDomain Type? foundGlobalConfig = AppDomain.CurrentDomain
.GetAssemblies() .GetAssemblies()
.SelectMany( .SelectMany(x => x.GetTypes())
x =>
x.GetTypes()
)
.FirstOrDefault( .FirstOrDefault(
x => x =>
x.IsAbstract == false && x.IsAbstract == false &&
x.IsInterface == false && x.IsInterface == false &&
x.IsPublic &&
iGlobalConfig.IsAssignableFrom(x) iGlobalConfig.IsAssignableFrom(x)
); );
_globalConfig = ObjectActivator.CreateInstance(foundGlobalConfig) as IGlobalConfig; if(foundGlobalConfig != null)
{
_globalConfig = ObjectActivator.CreateInstance(foundGlobalConfig) as IGlobalConfig;
}
if(_globalConfig == null)
{
_globalConfig = new DefaultGlobalConfig();
}
return _globalConfig; return _globalConfig;
} }
// TODO: Better default global config
private class DefaultGlobalConfig : IGlobalConfig
{
public string Title { get; } = string.Empty;
public string TitleSeparator { get; } = string.Empty;
public string Author { get; } = string.Empty;
public string Copyright { get; } = string.Empty;
public string DefaultHandler { get; } = string.Empty;
public string LoginHandler { get; } = string.Empty;
public List<string> AllowedExtensions { get; } = new List<string>();
public bool IsUserAuthenticated(HttpContext context)
{
return false;
}
public void UserUnauthenticate(HttpContext context)
{
}
}
} }
} }

View File

@@ -17,7 +17,7 @@ namespace VAR.WebFormsCore.Code
StringBuilder sbOutput = new StringBuilder(); StringBuilder sbOutput = new StringBuilder();
sbOutput.Append("<h2>Internal error</h2>"); sbOutput.Append("<h2>Internal error</h2>");
Exception exAux = ex; Exception? exAux = ex;
while (exAux != null) while (exAux != null)
{ {
sbOutput.AppendFormat("<p><b>Message:</b> {0}</p>", exAux.Message); sbOutput.AppendFormat("<p><b>Message:</b> {0}</p>", exAux.Message);

View File

@@ -68,7 +68,7 @@ namespace VAR.WebFormsCore.Code
} }
} }
IHttpHandler handler = GetHandler(file); IHttpHandler? handler = GetHandler(file);
if (handler == null) if (handler == null)
{ {
// TODO: FrmNotFound // TODO: FrmNotFound
@@ -81,16 +81,16 @@ namespace VAR.WebFormsCore.Code
private static readonly Dictionary<string, Type> Handlers = new Dictionary<string, Type>(); private static readonly Dictionary<string, Type> Handlers = new Dictionary<string, Type>();
private static IHttpHandler GetHandler(string typeName) private static IHttpHandler? GetHandler(string typeName)
{ {
if (string.IsNullOrEmpty(typeName)) { return null; } if (string.IsNullOrEmpty(typeName)) { return null; }
Type type; Type? type;
lock (Handlers) lock (Handlers)
{ {
if (Handlers.TryGetValue(typeName, out type)) if (Handlers.TryGetValue(typeName, out type))
{ {
IHttpHandler handler = ObjectActivator.CreateInstance(type) as IHttpHandler; IHttpHandler? handler = ObjectActivator.CreateInstance(type) as IHttpHandler;
return handler; return handler;
} }
} }
@@ -129,7 +129,7 @@ namespace VAR.WebFormsCore.Code
// Use found type // Use found type
if (type != null) if (type != null)
{ {
IHttpHandler handler = ObjectActivator.CreateInstance(type) as IHttpHandler; IHttpHandler? handler = ObjectActivator.CreateInstance(type) as IHttpHandler;
if (handler != null) if (handler != null)
{ {
lock (Handlers) lock (Handlers)

View File

@@ -12,7 +12,7 @@ namespace VAR.WebFormsCore.Code
string privatePath = Path.Combine(currentDir, baseDir); string privatePath = Path.Combine(currentDir, baseDir);
while (Directory.Exists(privatePath) == false) while (Directory.Exists(privatePath) == false)
{ {
DirectoryInfo dirInfo = Directory.GetParent(currentDir); DirectoryInfo? dirInfo = Directory.GetParent(currentDir);
if (dirInfo == null) { break; } if (dirInfo == null) { break; }
currentDir = dirInfo.FullName; currentDir = dirInfo.FullName;
@@ -22,11 +22,11 @@ namespace VAR.WebFormsCore.Code
return Path.Combine(privatePath, fileName); return Path.Combine(privatePath, fileName);
} }
private static Dictionary<string, Dictionary<string, object>> _literals; private static Dictionary<string, Dictionary<string, object>?>? _literals;
private static void InitializeLiterals() private static void InitializeLiterals()
{ {
_literals = new Dictionary<string, Dictionary<string, object>>(); _literals = new Dictionary<string, Dictionary<string, object>?>();
JsonParser jsonParser = new JsonParser(); JsonParser jsonParser = new JsonParser();
foreach (string lang in new[] {"en", "es"}) foreach (string lang in new[] {"en", "es"})
@@ -75,7 +75,7 @@ namespace VAR.WebFormsCore.Code
return DefaultLanguage; return DefaultLanguage;
} }
public static string GetLiteral(string resource, string culture = null) public static string GetLiteral(string resource, string? culture = null)
{ {
if (_literals == null) { InitializeLiterals(); } if (_literals == null) { InitializeLiterals(); }
@@ -83,7 +83,7 @@ namespace VAR.WebFormsCore.Code
if (_literals == null || _literals.ContainsKey(culture) == false) { return resource; } if (_literals == null || _literals.ContainsKey(culture) == false) { return resource; }
Dictionary<string, object> literalCurrentCulture = _literals[culture]; Dictionary<string, object>? literalCurrentCulture = _literals[culture];
if (literalCurrentCulture == null || literalCurrentCulture.ContainsKey(resource) == false) if (literalCurrentCulture == null || literalCurrentCulture.ContainsKey(resource) == false)
{ {

View File

@@ -5,7 +5,7 @@ namespace VAR.WebFormsCore.Code
{ {
public static class ServerHelpers public static class ServerHelpers
{ {
private static string _contentRoot; private static string? _contentRoot;
public static void SetContentRoot(string contentRoot) { _contentRoot = contentRoot; } public static void SetContentRoot(string contentRoot) { _contentRoot = contentRoot; }
public static string MapContentPath(string path) public static string MapContentPath(string path)

View File

@@ -71,7 +71,7 @@ namespace VAR.WebFormsCore.Code
public static async void ResponseStaticFile(HttpContext context, string filePath) public static async void ResponseStaticFile(HttpContext context, string filePath)
{ {
string extension = Path.GetExtension(filePath).ToLower(); string extension = Path.GetExtension(filePath).ToLower();
MimeTypeByExtension.TryGetValue(extension, out string contentType); MimeTypeByExtension.TryGetValue(extension, out string? contentType);
if (string.IsNullOrEmpty(contentType) == false) { context.Response.ContentType = contentType; } if (string.IsNullOrEmpty(contentType) == false) { context.Response.ContentType = contentType; }

View File

@@ -15,11 +15,11 @@ namespace VAR.WebFormsCore.Controls
set => _text = value; set => _text = value;
} }
public string OnClientClick { get; set; } public string OnClientClick { get; set; } = string.Empty;
public string CommandArgument { get; set; } = string.Empty; public string CommandArgument { get; set; } = string.Empty;
public event EventHandler Click; public event EventHandler? Click;
protected override void Render(TextWriter textWriter) protected override void Render(TextWriter textWriter)
{ {

View File

@@ -11,7 +11,7 @@ namespace VAR.WebFormsCore.Controls
private readonly TextBox _txtContent = new TextBox(); private readonly TextBox _txtContent = new TextBox();
private HiddenField _hidSize; private HiddenField? _hidSize;
private const string CssClassBase = "textbox"; private const string CssClassBase = "textbox";
private string _cssClassExtra = ""; private string _cssClassExtra = "";
@@ -22,7 +22,7 @@ namespace VAR.WebFormsCore.Controls
private bool _markedInvalid; private bool _markedInvalid;
private Control _nextFocusOnEnter; private Control? _nextFocusOnEnter;
private bool _keepSize; private bool _keepSize;
@@ -54,7 +54,7 @@ namespace VAR.WebFormsCore.Controls
set => _markedInvalid = value; set => _markedInvalid = value;
} }
public Control NextFocusOnEnter public Control? NextFocusOnEnter
{ {
get => _nextFocusOnEnter; get => _nextFocusOnEnter;
set => _nextFocusOnEnter = value; set => _nextFocusOnEnter = value;
@@ -88,7 +88,7 @@ namespace VAR.WebFormsCore.Controls
PreRender += CTextbox_PreRender; PreRender += CTextbox_PreRender;
} }
private void CTextBox_Init(object sender, EventArgs e) private void CTextBox_Init(object? sender, EventArgs e)
{ {
Controls.Add(_txtContent); Controls.Add(_txtContent);
@@ -103,7 +103,7 @@ namespace VAR.WebFormsCore.Controls
string strCfgName = $"{this.ClientID}_cfg"; string strCfgName = $"{this.ClientID}_cfg";
Dictionary<string, object> cfg = new Dictionary<string, object> Dictionary<string, object> cfg = new Dictionary<string, object>
{ {
{"txtContent", _txtContent.ClientID}, {"hidSize", _hidSize.ClientID}, {"keepSize", _keepSize}, {"txtContent", _txtContent.ClientID}, {"hidSize", _hidSize?.ClientID ?? string.Empty}, {"keepSize", _keepSize},
}; };
StringBuilder sbCfg = new StringBuilder(); StringBuilder sbCfg = new StringBuilder();
sbCfg.AppendFormat("<script>\n"); sbCfg.AppendFormat("<script>\n");
@@ -115,7 +115,7 @@ namespace VAR.WebFormsCore.Controls
} }
} }
private void CTextbox_PreRender(object sender, EventArgs e) private void CTextbox_PreRender(object? sender, EventArgs e)
{ {
_txtContent.CssClass = CssClassBase; _txtContent.CssClass = CssClassBase;
if (string.IsNullOrEmpty(_cssClassExtra) == false) if (string.IsNullOrEmpty(_cssClassExtra) == false)
@@ -123,7 +123,7 @@ namespace VAR.WebFormsCore.Controls
_txtContent.CssClass = $"{CssClassBase} {_cssClassExtra}"; _txtContent.CssClass = $"{CssClassBase} {_cssClassExtra}";
} }
if (Page.IsPostBack && (_allowEmpty == false && IsEmpty()) || _markedInvalid) if (Page?.IsPostBack == true && (_allowEmpty == false && IsEmpty()) || _markedInvalid)
{ {
_txtContent.CssClass += " textboxInvalid"; _txtContent.CssClass += " textboxInvalid";
} }
@@ -154,10 +154,10 @@ namespace VAR.WebFormsCore.Controls
public int? GetClientsideHeight() public int? GetClientsideHeight()
{ {
if (string.IsNullOrEmpty(_hidSize.Value)) { return null; } if (string.IsNullOrEmpty(_hidSize?.Value)) { return null; }
JsonParser jsonParser = new JsonParser(); JsonParser jsonParser = new JsonParser();
Dictionary<string, object> sizeObj = jsonParser.Parse(_hidSize.Value) as Dictionary<string, object>; Dictionary<string, object>? sizeObj = jsonParser.Parse(_hidSize.Value) as Dictionary<string, object>;
if (sizeObj == null) { return null; } if (sizeObj == null) { return null; }
if (sizeObj.ContainsKey("height") == false) { return null; } if (sizeObj.ContainsKey("height") == false) { return null; }
@@ -169,22 +169,28 @@ namespace VAR.WebFormsCore.Controls
{ {
if (height == null) if (height == null)
{ {
_hidSize.Value = string.Empty; if (_hidSize != null)
{
_hidSize.Value = string.Empty;
}
return; return;
} }
Dictionary<string, object> sizeObj; Dictionary<string, object?>? sizeObj = null;
if (string.IsNullOrEmpty(_hidSize.Value) == false) if (string.IsNullOrEmpty(_hidSize?.Value) == false)
{ {
JsonParser jsonParser = new JsonParser(); JsonParser jsonParser = new JsonParser();
sizeObj = jsonParser.Parse(_hidSize.Value) as Dictionary<string, object>; sizeObj = jsonParser.Parse(_hidSize.Value) as Dictionary<string, object?>;
} }
else if(sizeObj == null)
{ {
sizeObj = new Dictionary<string, object> {{"height", height}, {"width", null}, {"scrollTop", null},}; sizeObj = new Dictionary<string, object?> {{"height", height}, {"width", null}, {"scrollTop", null},};
} }
_hidSize.Value = JsonWriter.WriteObject(sizeObj); if (_hidSize != null)
{
_hidSize.Value = JsonWriter.WriteObject(sizeObj);
}
} }
#endregion Public methods #endregion Public methods

View File

@@ -9,7 +9,7 @@ namespace VAR.WebFormsCore.Controls
{ {
public class Control public class Control
{ {
public event EventHandler PreInit; public event EventHandler? PreInit;
protected void OnPreInit(EventArgs e) protected void OnPreInit(EventArgs e)
{ {
@@ -17,8 +17,7 @@ namespace VAR.WebFormsCore.Controls
foreach (Control control in Controls) { control.OnPreInit(e); } foreach (Control control in Controls) { control.OnPreInit(e); }
} }
public event EventHandler? Init;
public event EventHandler Init;
protected void OnInit(EventArgs e) protected void OnInit(EventArgs e)
{ {
@@ -27,7 +26,7 @@ namespace VAR.WebFormsCore.Controls
} }
public event EventHandler Load; public event EventHandler? Load;
protected virtual void Process() { } protected virtual void Process() { }
@@ -38,7 +37,7 @@ namespace VAR.WebFormsCore.Controls
foreach (Control control in Controls) { control.OnLoad(e); } foreach (Control control in Controls) { control.OnLoad(e); }
} }
public event EventHandler PreRender; public event EventHandler? PreRender;
protected void OnPreRender(EventArgs e) protected void OnPreRender(EventArgs e)
{ {
@@ -46,9 +45,9 @@ namespace VAR.WebFormsCore.Controls
foreach (Control control in Controls) { control.OnPreRender(e); } foreach (Control control in Controls) { control.OnPreRender(e); }
} }
private string _id; private string? _id;
public string ID public string? ID
{ {
get => _id; get => _id;
set set
@@ -58,16 +57,16 @@ namespace VAR.WebFormsCore.Controls
} }
} }
private string _clientID; private string? _clientID;
public string ClientID public string ClientID
{ {
get { return _clientID ??= GenerateClientID(); } get { return _clientID ??= GenerateClientID(); }
} }
private Control _parent; private Control? _parent;
public Control Parent public Control? Parent
{ {
get => _parent; get => _parent;
set set
@@ -77,18 +76,18 @@ namespace VAR.WebFormsCore.Controls
} }
} }
private ControlCollection _controls; private ControlCollection? _controls;
public string CssClass { get; set; } public string CssClass { get; set; } = string.Empty;
public ControlCollection Controls public ControlCollection Controls
{ {
get { return _controls ??= new ControlCollection(this); } get { return _controls ??= new ControlCollection(this); }
} }
private Page _page; private Page? _page;
public Page Page public Page? Page
{ {
get => _page; get => _page;
set set
@@ -111,7 +110,7 @@ namespace VAR.WebFormsCore.Controls
sbClientID.Insert(0, currentID); sbClientID.Insert(0, currentID);
} }
Control parent = Parent; Control? parent = Parent;
while (parent != null) while (parent != null)
{ {
if (parent is INamingContainer) if (parent is INamingContainer)
@@ -131,9 +130,9 @@ namespace VAR.WebFormsCore.Controls
return sbClientID.ToString(); return sbClientID.ToString();
} }
public Dictionary<string, string> Style { get; } = new Dictionary<string, string>(); public Dictionary<string, string> Style { get; } = new();
public Dictionary<string, string> Attributes { get; } = new Dictionary<string, string>(); public Dictionary<string, string> Attributes { get; } = new();
public int Index { get; set; } public int Index { get; set; }
@@ -179,13 +178,19 @@ namespace VAR.WebFormsCore.Controls
} }
} }
protected List<Control> ChildsOfType<T>(List<Control> controls = null) protected List<Control> ChildsOfType<T>(List<Control>? controls = null)
{ {
controls ??= new List<Control>(); controls ??= new List<Control>();
if (this is T) { controls.Add(this); } if (this is T) { controls.Add(this); }
foreach (Control child in _controls) { child.ChildsOfType<T>(controls); } if (_controls != null)
{
foreach (Control child in _controls)
{
child.ChildsOfType<T>(controls);
}
}
return controls; return controls;
} }

View File

@@ -14,9 +14,9 @@ namespace VAR.WebFormsCore.Controls
protected override void Process() protected override void Process()
{ {
if (Page.IsPostBack && Page.Context.Request.Form.ContainsKey(ClientID)) if (Page?.IsPostBack == true && Page?.Context?.Request.Form.ContainsKey(ClientID) == true)
{ {
Value = Page.Context.Request.Form[ClientID]; Value = Page?.Context.Request.Form[ClientID][0] ?? string.Empty;
} }
} }

View File

@@ -26,16 +26,19 @@ namespace VAR.WebFormsCore.Controls
private string GetAction() private string GetAction()
{ {
StringBuilder sbAction = new(); StringBuilder sbAction = new();
sbAction.Append(Page.GetType().Name); sbAction.Append(Page?.GetType().Name);
if (Page.Context.Request.Query.Count <= 0) { return sbAction.ToString(); } if ((Page?.Context?.Request.Query.Count ?? 0) <= 0) { return sbAction.ToString(); }
sbAction.Append('?'); sbAction.Append('?');
foreach (KeyValuePair<string, StringValues> queryParam in Page.Context.Request.Query) if (Page?.Context?.Request.Query != null)
{ {
string key = ServerHelpers.UrlEncode(queryParam.Key); foreach (KeyValuePair<string, StringValues> queryParam in Page.Context.Request.Query)
string value = ServerHelpers.UrlEncode(queryParam.Value[0]); {
sbAction.Append($"&{key}={value}"); string key = ServerHelpers.UrlEncode(queryParam.Key);
string value = ServerHelpers.UrlEncode(queryParam.Value[0] ?? string.Empty);
sbAction.Append($"&{key}={value}");
}
} }
return sbAction.ToString(); return sbAction.ToString();

View File

@@ -4,7 +4,7 @@ namespace VAR.WebFormsCore.Controls
{ {
public class HtmlHead : Control public class HtmlHead : Control
{ {
public string Title { get; set; } public string Title { get; set; } = string.Empty;
protected override void Render(TextWriter textWriter) protected override void Render(TextWriter textWriter)
{ {

View File

@@ -4,9 +4,9 @@ namespace VAR.WebFormsCore.Controls
{ {
public class HtmlMeta : Control public class HtmlMeta : Control
{ {
public string Name { get; init; } public string Name { get; init; } = string.Empty;
public string Content { get; init; } public string Content { get; init; } = string.Empty;
public string HttpEquiv { get; internal init; } public string HttpEquiv { get; internal init; } = string.Empty;
protected override void Render(TextWriter textWriter) protected override void Render(TextWriter textWriter)
{ {

View File

@@ -4,8 +4,8 @@ namespace VAR.WebFormsCore.Controls
{ {
public class HyperLink : Control public class HyperLink : Control
{ {
public string NavigateUrl { get; set; } public string NavigateUrl { get; set; } = string.Empty;
public string Text { get; init; } public string Text { get; init; } = string.Empty;
protected override void Render(TextWriter textWriter) protected override void Render(TextWriter textWriter)
{ {

View File

@@ -4,7 +4,7 @@ namespace VAR.WebFormsCore.Controls
{ {
public class LiteralControl : Control public class LiteralControl : Control
{ {
public string Content { get; set; } public string Content { get; set; } = string.Empty;
public LiteralControl() { } public LiteralControl() { }
public LiteralControl(string content) { Content = content; } public LiteralControl(string content) { Content = content; }

View File

@@ -11,9 +11,9 @@ namespace VAR.WebFormsCore.Controls
protected override void Process() protected override void Process()
{ {
if (Page.IsPostBack && Page.Context.Request.Form.ContainsKey(ClientID)) if (Page?.IsPostBack == true && Page?.Context?.Request.Form.ContainsKey(ClientID) == true)
{ {
Text = Page.Context.Request.Form[ClientID]; Text = Page?.Context.Request.Form[ClientID][0] ?? string.Empty;
} }
} }

View File

@@ -4,7 +4,7 @@ namespace VAR.WebFormsCore.Pages
{ {
public static class FormUtils public static class FormUtils
{ {
public static Control CreatePanel(string cssClass, Control ctrl = null) public static Control CreatePanel(string cssClass, Control? ctrl = null)
{ {
Panel pnl = new Panel(); Panel pnl = new Panel();
if (ctrl != null) { pnl.Controls.Add(ctrl); } if (ctrl != null) { pnl.Controls.Add(ctrl); }

View File

@@ -21,7 +21,7 @@ namespace VAR.WebFormsCore.Pages
Init += FrmError_Init; Init += FrmError_Init;
} }
private void FrmError_Init(object sender, EventArgs e) { InitializeControls(); } private void FrmError_Init(object? sender, EventArgs e) { InitializeControls(); }
#endregion Page life cycle #endregion Page life cycle
@@ -34,7 +34,7 @@ namespace VAR.WebFormsCore.Pages
Label lblErrorTitle = new Label {Text = Title, Tag = "h2"}; Label lblErrorTitle = new Label {Text = Title, Tag = "h2"};
Controls.Add(lblErrorTitle); Controls.Add(lblErrorTitle);
Exception exAux = _ex; Exception? exAux = _ex;
//if (exAux is HttpUnhandledException && exAux.InnerException != null) { exAux = exAux.InnerException; } //if (exAux is HttpUnhandledException && exAux.InnerException != null) { exAux = exAux.InnerException; }
while (exAux != null) while (exAux != null)
{ {

View File

@@ -10,9 +10,9 @@ namespace VAR.WebFormsCore.Pages
{ {
public class Page : Control, IHttpHandler public class Page : Control, IHttpHandler
{ {
protected string Title { get; set; } protected string Title { get; set; } = string.Empty;
public HttpContext Context { get; private set; } public HttpContext? Context { get; private set; }
private static readonly Encoding Utf8Encoding = new UTF8Encoding(); private static readonly Encoding Utf8Encoding = new UTF8Encoding();

View File

@@ -9,12 +9,12 @@ namespace VAR.WebFormsCore.Pages
{ {
#region Declarations #region Declarations
private HtmlHead _head; private readonly HtmlHead _head = new();
private HtmlBody _body; private readonly HtmlBody _body = new();
private HtmlForm _form; private readonly HtmlForm _form = new() {ID = "formMain"};
private readonly Panel _pnlContainer = new Panel(); private readonly Panel _pnlContainer = new();
private readonly Button _btnPostback = new Button(); private readonly Button _btnPostback = new();
private readonly Button _btnLogout = new Button(); private readonly Button _btnLogout = new();
private bool _isAuthenticated; private bool _isAuthenticated;
@@ -37,20 +37,24 @@ namespace VAR.WebFormsCore.Pages
PreRender += PageCommon_PreRender; PreRender += PageCommon_PreRender;
} }
private void PageCommon_PreInit(object sender, EventArgs e) private void PageCommon_PreInit(object? sender, EventArgs e)
{ {
Context.Response.PrepareUncacheableResponse(); Context?.Response.PrepareUncacheableResponse();
if (Context != null)
{
_isAuthenticated = GlobalConfig.Get().IsUserAuthenticated(Context);
}
_isAuthenticated = GlobalConfig.Get().IsUserAuthenticated(Context);
if (MustBeAuthenticated && _isAuthenticated == false) if (MustBeAuthenticated && _isAuthenticated == false)
{ {
Context.Response.Redirect(GlobalConfig.Get().LoginHandler); Context?.Response.Redirect(GlobalConfig.Get().LoginHandler);
} }
} }
private void PageCommon_Init(object sender, EventArgs e) { CreateControls(); } private void PageCommon_Init(object? sender, EventArgs e) { CreateControls(); }
private void PageCommon_PreRender(object sender, EventArgs e) private void PageCommon_PreRender(object? sender, EventArgs e)
{ {
_head.Title = string.IsNullOrEmpty(Title) _head.Title = string.IsNullOrEmpty(Title)
? GlobalConfig.Get().Title ? GlobalConfig.Get().Title
@@ -62,10 +66,13 @@ namespace VAR.WebFormsCore.Pages
#region UI Events #region UI Events
private void btnLogout_Click(object sender, EventArgs e) private void btnLogout_Click(object? sender, EventArgs e)
{ {
GlobalConfig.Get().UserUnauthenticate(Context); if(Context != null)
if (MustBeAuthenticated) { Context.Response.Redirect(GlobalConfig.Get().LoginHandler); } {
GlobalConfig.Get().UserUnauthenticate(Context);
}
if (MustBeAuthenticated) { Context?.Response.Redirect(GlobalConfig.Get().LoginHandler); }
} }
#endregion UI Events #endregion UI Events
@@ -82,7 +89,6 @@ namespace VAR.WebFormsCore.Pages
var html = new HtmlGenericControl("html"); var html = new HtmlGenericControl("html");
base.Controls.Add(html); base.Controls.Add(html);
_head = new HtmlHead();
html.Controls.Add(_head); html.Controls.Add(_head);
_head.Controls.Add(new HtmlMeta {HttpEquiv = "X-UA-Compatible", Content = "IE=Edge"}); _head.Controls.Add(new HtmlMeta {HttpEquiv = "X-UA-Compatible", Content = "IE=Edge"});
@@ -97,7 +103,7 @@ namespace VAR.WebFormsCore.Pages
} }
); );
string version = Assembly.GetExecutingAssembly().GetName().Version?.ToString(); string? version = Assembly.GetExecutingAssembly().GetName().Version?.ToString();
_head.Controls.Add( _head.Controls.Add(
new LiteralControl($"<script type=\"text/javascript\" src=\"ScriptsBundler?v={version}\"></script>\n") new LiteralControl($"<script type=\"text/javascript\" src=\"ScriptsBundler?v={version}\"></script>\n")
); );
@@ -105,9 +111,7 @@ namespace VAR.WebFormsCore.Pages
new LiteralControl($"<link href=\"StylesBundler?v={version}\" type=\"text/css\" rel=\"stylesheet\"/>\n") new LiteralControl($"<link href=\"StylesBundler?v={version}\" type=\"text/css\" rel=\"stylesheet\"/>\n")
); );
_body = new HtmlBody();
html.Controls.Add(_body); html.Controls.Add(_body);
_form = new HtmlForm {ID = "formMain"};
_body.Controls.Add(_form); _body.Controls.Add(_form);
var pnlHeader = new Panel {CssClass = "divHeader"}; var pnlHeader = new Panel {CssClass = "divHeader"};

View File

@@ -3,6 +3,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>