5

In ASP.NET MVC we have below

Request.ServerVariables.AllKeys
Request.ServerVariables.GetValues("REMOTE_ADDR")[0]
Request.ServerVariables.GetValues("HTTP_SCGID")[0]
Request.ServerVariables.GetValues("HTTP_SCMAIL")[0]

I am looking for equivalent in ASP.NET Core Razor Pages.

Dale K
  • 25,246
  • 15
  • 42
  • 71
reddy
  • 173
  • 1
  • 4
  • 9
  • 3
    Possible duplicate of [How to access ServerVariables in AspnetCore 1.0](https://stackoverflow.com/questions/38429604/how-to-access-servervariables-in-aspnetcore-1-0) – FaizanHussainRabbani Feb 21 '18 at 12:54
  • @FaizanRabbani that link didn't help me much. – reddy Feb 21 '18 at 13:16
  • This may be of use with regards to REMOTE_ADDR, although the post is a bit old now: [DIRECT replacement of UserHostAddress in ASP.NET Core?](https://stackoverflow.com/questions/35794237/direct-replacement-of-userhostaddress-in-asp-net-core) – SpruceMoose Feb 21 '18 at 14:05

1 Answers1

1

To see headers use

          foreach (var item in HttpContext.Request.Headers)
          {
            Console.WriteLine($"{item.Key} | {item.Value}");
          }

To get value from a single header you can use

HttpContext.Request.Headers["User-Agent"].FirstOrDefault()

Remote address:

HttpContext.Connection.RemoteIpAddress.ToString()

Also Check out

HttpContext.Request.GetTypedHeaders().Referer

If you just type HttpContext.Request.GetTypedHeaders(). see what values VS IntelliSense shows you as available:

enter image description here

adinas
  • 4,150
  • 3
  • 37
  • 47