Skip to content
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

Add a setter for HttpContext.Current #416

Merged
merged 3 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ internal HttpContext() { }
public System.Web.HttpApplicationState Application { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public System.Web.HttpApplication ApplicationInstance { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public System.Web.Caching.Cache Cache { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public static System.Web.HttpContext Current { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public static System.Web.HttpContext Current { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} set { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public System.Web.RequestNotification CurrentNotification { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public System.Exception Error { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
public bool IsDebuggingEnabled { get { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");} }
Expand Down
6 changes: 5 additions & 1 deletion src/Microsoft.AspNetCore.SystemWebAdapters/HttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public class HttpContext : IServiceProvider
private IDictionary? _items;
private TraceContext? _trace;

public static HttpContext? Current => _accessor.HttpContext;
public static HttpContext? Current
{
get => _accessor.HttpContext;
set => _accessor.HttpContext = value;
}

internal HttpContext(HttpContextCore context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,5 +471,35 @@ public void RewritePathWithPathInfo(bool rebase)
// Assert
feature.Verify(f => f.Rewrite(filePath, pathInfo, query, rebase), Times.Once);
}

[Fact]
public void SetHttpContext()
{
// Arrange
var coreContext = new DefaultHttpContext();
var context = new HttpContext(coreContext);

// Act
HttpContext.Current = context;

// Assert
Assert.IsType<HttpContext>(HttpContext.Current);
}


[Fact]
public void SetHttpContextToNull()
{
// Arrange
var coreContext = new DefaultHttpContext();
var context = new HttpContext(coreContext);
HttpContext.Current = context;

// Act
HttpContext.Current = null;

// Assert
Assert.Null(HttpContext.Current);
}
}
}