Skip to content

System.Web adapter API to convert between ASP.NET Core and System.Web types #51119

@twsouthwick

Description

@twsouthwick

Background and Motivation

Through Microsoft.AspNetCore.SystemWebAdapters, we can get a representation of the HttpContext that conforms to what people expect for System.Web.HttpContext. Currently, the way to do that is via implicit conversions. It would be nice to have a static method that can be called to convert it without needing to cast in times where implicit conversions aren't obvious.

For example, currently we need to do the following if we want to change views in a method:

public void Test(Microsoft.AspNetCore.Http.HttpContext contextCore)
{
  var context = (System.Web.HttpContext)contextCore;
}

Proposed API

using System.Diagnostics.CodeAnalysis;
using System.Web;

namespace Microsoft.AspNetCore.SystemWebAdapters;

public static class SystemWebAdapterExtensions
{
    [return: NotNullIfNotNull(nameof(request))]
    public static HttpRequest? GetSystemWebRequest(this Microsoft.AspNetCore.Http.HttpRequest? request);

    [return: NotNullIfNotNull(nameof(request))]
    public static HttpRequestBase? GetSystemWebRequestBase(this Microsoft.AspNetCore.Http.HttpRequest? request);

    [return: NotNullIfNotNull(nameof(request))]
    public static Microsoft.AspNetCore.Http.HttpRequest? GetAspNetCoreRequest(this HttpRequest? request);

    [return: NotNullIfNotNull(nameof(response))]
    public static HttpResponse? GetSystemWebResponse(this Microsoft.AspNetCore.Http.HttpResponse? response);

    [return: NotNullIfNotNull(nameof(response))]
    public static HttpResponseBase? GetSystemWebResponseBase(this Microsoft.AspNetCore.Http.HttpResponse? response);

    [return: NotNullIfNotNull(nameof(response))]
    public static Microsoft.AspNetCore.Http.HttpResponse? GetAspNetCoreResponse(this HttpResponse? response);

    [return: NotNullIfNotNull(nameof(context))]
    public static HttpContext? GetSystemWebHttpContext(this Microsoft.AspNetCore.Http.HttpContext? context);

    [return: NotNullIfNotNull(nameof(context))]
    public static Microsoft.AspNetCore.Http.HttpContext? GetAspNetCoreHttpContext(this HttpContext? context);

    [return: NotNullIfNotNull(nameof(context))]
    public static HttpContextBase? GetSystemWebHttpContextBase(this Microsoft.AspNetCore.Http.HttpContext? context);
}

Usage Examples

public void Test(Microsoft.AspNetCore.Http.HttpContext contextCore)
{
-  var context = (System.Web.HttpContext)contextCore;
+  var context = contextCore.GetSystemWebHttpContext();

-  var request = (System.Web.HttpRequest)contextCore.Request;
+  var request = contextCore.Request.GetSystemWebHttpRequest();
}

See implementation at dotnet/systemweb-adapters#353

Alternative Designs

Unsure of names here, or prefix (i.e. Get or As)

For example, for HttpContext conversions:

  • GetSystemWebHttpContext()/GetCoreHttpContext()
  • GetHttpContextAdapter()/GetHttpContextAdapter()
  • AsSystemWebHttpContext()/AsCoreHttpContext()
  • AsHttpContextAdapter()/AsHttpContextAdapter()
  • ToSystemWebHttpContext()/ToCoreHttpContext()
  • ToHttpContextAdapter()/ToHttpContextAdapter()

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions