-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Add System.Web.IHtmlString and System.Web.HtmlString to System.Web.HttpUtility #83477
Comments
Tagging subscribers to this area: @dotnet/ncl Issue DetailsBackground and motivationWe have created https://github.com/dotnet/systemweb-adapters to provide APIs people used from This proposal is to add this type and the default implementaiton of it back and update API Proposalnamespace System.Web;
public interface IHtmlString
{
string ToHtmlString();
}
public class HtmlString : IHtmlString
{
public HtmlString(string value);
public string ToHtmlString();
public override string ToString();
} API Usageusing System;
using System.Web;
var str = "<hello />";
var htmlString = new HtmlString(str);
Console.WriteLine(HttpUtility.HtmlEncode(str));
Console.WriteLine(HttpUtility.HtmlEncode(htmlString)); Outputs the following:
Alternative DesignsNo response RisksNo response
|
cc: @karelz do you know who the right person to look into this is? |
@terrajobst Thoughts on this proposal? This is to facilitate ASP.NET to ASP.NET Core migration. |
Porting the interface (and the extra line or two in HttpUtility to recognize it) seems reasonable. Is the class necessary though? ASP.NET already has Microsoft.AspNetCore.Html.HtmlString... can it just implement IHtmlString if added? |
@stephentoub @twsouthwick I think the class just makes porting code slightly easier, the interface and the realignment of the |
Do we need the |
Yes - it's a .NET Framework API we'd like to enable for compat reasons to simplify migration. If we don't have that method, the interface would be different and wouldn't work for compat
It's not as necessary as the interface. As @CZEMacLeod mentioned, that's something we could put in the System.Web Adapters project or people could probably define it themselves. The goal of adding this is purely to simplify the challenges people have with migration without requiring them to bring in a whole new stack (ASP.NET Core) to potentially low levels of their project and then figure out how to keep support framework and core. This is the only API we've been trying to enable for users that has requirements in existing BCL types - everything else is just in the Microsoft.AspNetCore.SystemWebAdapters package, which has no other dependencies and easy to add at lower levels for migration. |
Considering the interest and the low implementation cost, I added this to 8.0 and marked it ready-for-review. @dotnet/ncl let me know if there are any objections. |
My concern is simply there's now already something called HtmlString in .NET. This would introduce a second one with the same name, in which case it could actually make porting harder if someone ends up with usings for System.Web and Microsoft.AspNetCore.Html and compilation fails because of ambiguities. No additional references are required to use this type, as it's part of the ASP.NET shared framework, it already provides the exact same ctor ( If this isn't an issue, I don't personally have a problem adding the additional tiny type. Though if there's already a compat library that's used to enable other such porting-related things (and it sounds like there is), I'd prefer if this type is just added there if needed. |
@stephentoub I'm fine going forward with that. Sounds like to enable the scenarios we care about, we want to:
|
@GrabYourPitchforks @Eilon who might have more context on this. |
I don't have context on this specific request. But I can speak to the backstory of this type.
We basically set a goal for ourselves that we wanted to tell people to stop using Later, when Razor came along, the The type we had always intended people to use within aspnet Full Framework was the concrete |
@halter73 Are default interface implementations used in public ASP.NET Core APIs? If so, the following would enable all implementations of namespace Microsoft.AspNetCore.Html;
public interface IHtmlContent
+ : System.Web.IHtmlString
{
void WriteTo(TextWriter writer, HtmlEncoder encoder);
+ string System.Web.IHtmlString.ToHtmlString()
+ {
+ using var writer = new StringWriter();
+ WriteTo(writer, HtmlEncoder.Default);
+ return writer.ToString();
+ }
} |
@stephentoub is this only in-box for .NET 8? Or is there a package that supports .NET Standard for this assembly? I don't see anything obvious on NuGet, but thought I'd check. If not, then I'll look into adding an interface of |
Correct. We don't ship a separate package with HttpUtility. |
Edit by @antonfirsov: Made
class HtmlString
optional.Background and motivation
We have created https://github.com/dotnet/systemweb-adapters to provide APIs people used from
System.Web
backed by ASP.NET Core types to enable quicker migration to ASP.NET Core. Many of the APIs are ones we can provide there. However,System.Web.IHtmlString
was commonly used in ASP.NET Framework and expectedHttpUtility.HtmlEncode(object)
to special case it (see ReferenceSource).This proposal is to add this type and the default implementation of it back and update
HttpUtility.HtmlEncode
to special case it so that code that is being migrated from ASP.NET Framework will continue to work without having to rewrite it (or find unexpected bugs at some point).API Proposal
API Usage
Outputs the following:
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: