Skip to content

Instantly share code, notes, and snippets.

@xximjasonxx
Created October 25, 2020 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xximjasonxx/a6bc2ca30663e9e078c44000188fd381 to your computer and use it in GitHub Desktop.
Save xximjasonxx/a6bc2ca30663e9e078c44000188fd381 to your computer and use it in GitHub Desktop.
public class ContextAccessor : IContextAccessor
{
public IDictionary<string, string> QueryString
{
// assume .AsDictionary() is an extension method that takes the QueryString struct and converts it to a Dictionary
get { return HttpContext.Current.Request.QueryString.AsDictionary(); }
}
}
public interface IContextAccessor
{
IDictionary<string, string> QueryString { get; }
}
[ApiController]
[Route("api/test")
public class TestController : ControllerBase
{
private readonly IContextAccessor _contextAccessor { get; set; }
public TestController(IContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;
}
public IActionResult Get()
{
return Ok(_contextAccessor.QueryString["name"]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment