dotnet / dotnet/systemweb-adapters

Proposal to add HtmlEncode to HttpServerUtility

Open
#683 3 comments 1 reaction 0 assignees View on GitHub
up for grabs
Dominant language
C#
Stars
384
Forks
76
Avg merge
2h 24m
Merged PRs (30d)
1

Description

## Summary

Add `HtmlEncode` overloads to [HttpServerUtility](https://github.com/dotnet/systemweb-adapters/blob/main/src/Microsoft.AspNetCore.SystemWebAdapters/HttpServerUtility.cs) (and its Base/Wrapper mirrors) so code migrating from `System.Web` that calls `Server.HtmlEncode(...)` compiles and runs against the adapters without changes.
I was thinking of using `WebUtility.HtmlEncode` in the methods but do you think it would be better to use `HttpUtility.HtmlEncode` instead?

## Motivation and goals

We are migrating an ASP.NET application to [CoreWebForms](https://github.com/CoreWebForms/CoreWebForms). We use [Telerik UI](https://www.telerik.com/products/aspnet-ajax/documentation/introduction) and some of their components such as `RadAjaxLoadingPanel` depends on using `System.Web.HttpServerUtility.HtmlEncode(System.String)`. Adding `HtmlEncode` to `HttpServerUtility` would help us greatly with the migration.

Example:
```
System.MissingMethodException: Method not found: 'System.String System.Web.HttpServerUtility.HtmlEncode(System.String)'.
at Telerik.Web.SkinRegistrar.RegisterStyleSheetReference(ISkinnableControl control, StyleSheetReference reference, StringBuilder cssRegs, Type controlToRegisterType)
```

## In scope

- `string? HtmlEncode(string? value)` on `HttpServerUtility`, returning the encoded string (annotated `[NotNullIfNotNull("value")]`).
- `void HtmlEncode(string? value, TextWriter output)` on `HttpServerUtility`, writing directly to a `TextWriter`.
- Matching virtual declarations on `HttpServerUtilityBase` and pass-through overrides on `HttpServerUtilityWrapper`.
- Backing implementation delegated to `System.Net.WebUtility.HtmlEncode`.

## Out of scope

I was thinking not to include `HtmlDecode` overloads here but if you think it should be done simultaneously we can do it.

## Risks / unknowns

- [WebUtility](https://github.com/microsoft/referencesource/blob/main/System.Web/httpserverutility.cs#L960) or [HttpUtility](https://github.com/microsoft/referencesource/blob/main/System.Web/httpserverutility.cs#L1100)
- Should we use the signature of the old method `string HtmlEncode(string s)` or match the signature to `WebUtilitys HtmlEncode string? HtmlEncode(string? value)`
- Unsure about other unknowns or risks. Old [HttpServerUtility](https://github.com/microsoft/referencesource/blob/main/System.Web/httpserverutility.cs#L972) had these methods.

## Examples

- `HttpContext.Current.Server.HtmlEncode(value)`
- `System.Web.HttpServerUtility.HtmlEncode(value)`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.