如何在AspnetCore 1.0中访问ServerVariables [英] How to access ServerVariables in AspnetCore 1.0

查看:274
本文介绍了如何在AspnetCore 1.0中访问ServerVariables的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在现有的.Net网站中,使用

访问服务器变量.

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

如何在AspnetCore 1.0 Web应用程序中访问ServerVariables? 在控制器内部调试时,this.HttpContext.Features不包含IServerVariablesFeature.

解决方案

好的,我不会直接回答您的问题.我将尝试阐明为什么此服务器变量不再是问题.

"HTTP_ACCEPT_LANGUAGE"是IIS& .NET用于促进在ASP.NET框架上与应用程序通信内容语言.

过去,浏览器不是一致的,也没有一致地传递Accept-Language标头. 为了弥补这一空白,IIS等应用程序服务器必须通过智能地进行设置来弥补它,方法是使用标头,用户代理字符串和服务器上的默认配置的组合来组成与应用程序相关的内容.

出于某些原因,我们不再想要它了,

  1. 几乎所有浏览器都设置了Accept-Language标头.您只需在喜爱的浏览器的devtools中转到网络"选项卡并检查HTTP请求标头即可看到此信息.

    较新版本的.NET中的
  1. Http Request消息类非常理智并且易于阅读.

    在以下位置请求消息Windows .NET Framework

    dotnet中的HttpRequest类核心

  2. 与在Web服务器中编写的一些复杂的不透明逻辑相比,可以简化基于请求中的标头的内容服务.在所有应用程序变得更轻巧之后,服务器也变得更轻巧,它变得轻巧透明是值得的.为什么有人要在Web服务器中编写复杂的逻辑,而不是真正成为Web服务器.

因此,应用程序可以简单地检查Request Header集合.

再扩大一点, 使用dotnet核心,可以公开许多功能,Web服务器的实现可以支持这些功能.可以在此处找到.

>

可以帮助在这里找到

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 .

解决方案

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.

  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

这篇关于如何在AspnetCore 1.0中访问ServerVariables的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆