diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters/Generated/ExcludedApis.txt b/src/Microsoft.AspNetCore.SystemWebAdapters/Generated/ExcludedApis.txt index 3f67613e8a..b885231f50 100644 --- a/src/Microsoft.AspNetCore.SystemWebAdapters/Generated/ExcludedApis.txt +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/Generated/ExcludedApis.txt @@ -17,3 +17,9 @@ T:System.Web.SameSiteMode T:Microsoft.AspNetCore.SystemWebAdapters.IBrowserCapabilitiesFactory T:Microsoft.AspNetCore.SystemWebAdapters.IHttpBrowserCapabilityFeature + +# We provide implementations of this on .NET Standard 2.0 and .NET 6.0 +T:System.Web.IHtmlString + +# We implement extra interfaces that end up getting added to the generated stuff +T:System.Web.HtmlString diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters/HtmlString.IHtmlContent.cs b/src/Microsoft.AspNetCore.SystemWebAdapters/HtmlString.IHtmlContent.cs new file mode 100644 index 0000000000..df03c98890 --- /dev/null +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/HtmlString.IHtmlContent.cs @@ -0,0 +1,30 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#if NET6_0_OR_GREATER +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Text.Encodings.Web; +using Microsoft.AspNetCore.Html; + +using CoreHtmlString = Microsoft.AspNetCore.Html.HtmlString; + +namespace System.Web; + +public partial class HtmlString : IHtmlContent +{ + [SuppressMessage("Design", "CA1033:Interface methods should be callable by child types", Justification = Constants.ApiFromAspNet)] + void IHtmlContent.WriteTo(TextWriter writer, HtmlEncoder encoder) + { + writer.Write(_htmlString); + } + + [return: NotNullIfNotNull(nameof(other))] + public static implicit operator HtmlString?(CoreHtmlString? other) + => other is null ? null : new(other.Value ?? string.Empty); + + [return: NotNullIfNotNull(nameof(other))] + public static implicit operator CoreHtmlString?(HtmlString? other) + => other is null ? null : new(other._htmlString); +} +#endif diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters/HtmlString.cs b/src/Microsoft.AspNetCore.SystemWebAdapters/HtmlString.cs new file mode 100644 index 0000000000..eccd707fd5 --- /dev/null +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/HtmlString.cs @@ -0,0 +1,19 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#if NETFRAMEWORK +[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.HtmlString))] +#else +namespace System.Web; + +public partial class HtmlString : IHtmlString +{ + private readonly string _htmlString; + + public HtmlString(string value) => _htmlString = value; + + public string ToHtmlString() => _htmlString; + + public override string ToString() => _htmlString; +} +#endif diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters/IHtmlString.cs b/src/Microsoft.AspNetCore.SystemWebAdapters/IHtmlString.cs new file mode 100644 index 0000000000..3b02446f6d --- /dev/null +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/IHtmlString.cs @@ -0,0 +1,18 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +// This is available in-box in .NET Framework and .NET 8+. We type forward to that when possible, otherwise, provide an implementation here for compat purposes. +#if NETSTANDARD || NET6_0 + +namespace System.Web; + +public interface IHtmlString +{ + string ToHtmlString(); +} + +#else + +[assembly:System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Web.IHtmlString))] + +#endif diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters/Microsoft.AspNetCore.SystemWebAdapters.csproj b/src/Microsoft.AspNetCore.SystemWebAdapters/Microsoft.AspNetCore.SystemWebAdapters.csproj index ceeeaefc58..8f477d1d43 100644 --- a/src/Microsoft.AspNetCore.SystemWebAdapters/Microsoft.AspNetCore.SystemWebAdapters.csproj +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/Microsoft.AspNetCore.SystemWebAdapters.csproj @@ -28,6 +28,8 @@ + + @@ -45,6 +47,9 @@ + + + all runtime; build; native; contentfiles; analyzers