How to access ServerVariables in AspnetCore 1.0
Asked Answered
T

2

6

In exisiting .Net Web Site, Server Variables is accessed using

HttpContext.Current.Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"]

How to access the ServerVariables in AspnetCore 1.0 Web Application?
While debugging inside controller, this.HttpContext.Features does not contain IServerVariablesFeature .

Tarter answered 18/7, 2016 at 6:8 Comment(0)
S
8

Okay, I am not going to directly answer your question. I am going to try and throw some light on why this server variable is not a problem anymore.

"HTTP_ACCEPT_LANGUAGE" is a server variable that IIS & .NET used to facilitate on ASP.NET framework to communicate content language with the application.

Back in the days, browsers weren't consistent and did not pass Accept-Language headers consistently. To fill that gap, application servers such as IIS had to make it up by intelligently setting this up, by using the combination of headers, user agent string and default configuration on the server to make up something that is relevant for the application.

There are few reasons we don't want it anymore,

  1. Almost all browsers set Accept-Language header. You can see this by simply going to Network tab in devtools of your favorite browser and inspecting HTTP request headers.

Chrome browser showing accept language

  1. Http Request Message class in newer versions of .NET are very sane and clear to read.

    Request Message in windows .NET framework

    HttpRequest class in dotnet core

  2. It may simplify be easy to serve content based on the header in the request compared to some complex opaque logic written in the web server. After all the applications are getting lighter and so are the servers, it pays to be light and transparent. Why would somebody wants to write a complex logic in the webserver which it is not about really being a web server.

So, applications can simply inspect the Request Header collection.

Expanding on it a little more, With dotnet core, there are number of features that are exposed, which the implementing of the web server can support. Details can be found here.

More details that will help understand how the framework and webservers are neatly decoupled can be found here

Somewise answered 18/7, 2016 at 11:49 Comment(0)
C
3

You can't because the app now runs out of process. However, as humblelistener pointed out most of this information is available in other places. Was Accept Language the only one you needed?

Colorado answered 18/7, 2016 at 14:10 Comment(6)
these values are required HTTP_ACCEPT_LANGUAGE, HTTP_X_FORWARDED_FOR, REMOTE_ADDRTarter
HTTP_ACCEPT_LANGUAGE is the Accept-Language header, HTTP_X_FORWARDED_FOR is the X-Fowarded-For header (which you can use the ForwardedHeaders middleware to apply for you), and REMOTE_ADDR is HttpContext.Connection.RemoteIpAddressColorado
Do you know what field is CERT_SUBJECT and what it is mapped to?Allotrope
HttpContext.Connection.ClientCertificate.SubjectColorado
@Somewise - While this is helpful for some items, the headers do not contain the REMOTE_USER value which was previously available in ServerVariables()Kell
What about HttpContext.User?Colorado

© 2022 - 2024 — McMap. All rights reserved.