我需要HttpContext在静态方法或实用程序服务中访问当前.
使用经典的ASP.NET MVC System.Web,我只是用来HttpContext.Current静态访问上下文.但是我如何在ASP.NET Core中执行此操作?
在ASP.NET Core RC 1中,我使用以下代码检索上下文的值(页面的完整地址).然后我在配置中记录了这个值.
public class Startup
{
public IConfigurationRoot Configuration { get; set; }
private IHostingEnvironment CurrentEnvironment { get; set; }
private IHttpContextAccessor HttpContextAccessor { get; set; }
public Startup(IHostingEnvironment env,
IHttpContextAccessor httpContextAccessor)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
CurrentEnvironment = env;
HttpContextAccessor = httpContextAccessor;
}
public void ConfigureServices(IServiceCollection services)
{
...
services.AddOptions();
services.Configure<WebAppSettings>(configuration);
services.Configure<WebAppSettings>(settings =>
{
...
settings.WebRootPath = CurrentEnvironment.WebRootPath; …Run Code Online (Sandbox Code Playgroud) 我正在尝试捕获原始请求数据以进行问责,并希望将请求正文内容从Request对象中提取出来.
我看过做Request.InputStream的建议,但这个方法在Request对象上不可用.
知道如何获取Request.Content主体的字符串表示?
在早期版本的Asp.Net会话中,可以在任何页面中访问,如静态变量使用
System.Web.HttpContext.Current.Session["key"]
Run Code Online (Sandbox Code Playgroud)
在Asp.Net Core中,如何访问通过控制器调用的不同类中的会话,而不将会话属性作为所有类的构造函数中的附加参数传递
我无法访问控制器外部的会话变量,他们建议您添加超过 200 个示例;
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddHttpContextAccessor();
Run Code Online (Sandbox Code Playgroud)
并使用
public class DummyReference
{
private IHttpContextAccessor _httpContextAccessor;
public DummyReference(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public void DoSomething()
{
// access _httpcontextaccessor to reach sessions variables
}
}
Run Code Online (Sandbox Code Playgroud)
但是,没有人提到如何从我的控制器调用此类。我怎样才能到达那个班级?
如果将其更改为静态,那么我需要绕过构造。如果我创建它,我需要 httpcontextaccessor 来构造。
对于想要了解更多为什么我这样做的人,我想编写包含加密、解密数据库表 RowID 之类的方法的类,以便使用 value+sessionvariable 在 VIEW 中进行屏蔽,以确保其不被修改。
另外,我希望 DummyReference 是静态的,这样我可以轻松访问 DummyReference.EncryptValue 或 DecryptValue。
我需要访问HttpContext.CurrentASP.NET Core 2.2 中的类库
HttpContext.Current.Request.Url.ToString().Contains("SAMLart")
Run Code Online (Sandbox Code Playgroud)
当我尝试仅尝试使用 HomeController 中的方法时,我正在尝试移植代码,但在主 Web 项目中HttpContext甚至没有移植代码。Current
c# httpcontext asp.net-core-mvc asp.net-core asp.net-core-2.0
我的公司正在探索将新的.NET Core用于一个小型内部项目,但是,有一些非常复杂(和旧)的经典ASP页面我们现在不想重写.
在.NET Core解决方案中是否可以运行一些经典的ASP页面?到目前为止我们还没有管理,但也许我们缺少一些Program.cs或appsettings.json配置.
我一直在尝试在ASP.NET MVC和system.web中实现http上下文,它只是允许我使用HttpContext.Current来访问上下文.无论如何,我开始在StartUp类的configureService方法中注入IhttpcontextAccessor.我发布此帖以查看是否有人使用.Net Core 2.0实现了此功能.如果是这样,请随时分享知识.提前致谢.
services.AddSingleton<HttpContextAccessor, HttpContextAccessor>();
Run Code Online (Sandbox Code Playgroud) 我没有看到太多有关将 asp.net mvc 4.6 应用程序移植到 asp.net core 2.1 吨 1.0 到 2.0 和 2.0 的信息。到 2.1 文章,但变化有点陡峭。
好吧,我已经浏览过 google 和 stackoverflow,我正在尝试/想要做的就是转换旧的 .net 应用程序并将其引入 .net core。
我认为问题的一些罪魁祸首如下:
HttpContext.Current
HttpContext.Current.Request.Url.AbsolutePath
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)
private static string CollectionToHtmlTable(HttpCookieCollection collection)
Run Code Online (Sandbox Code Playgroud)
我看到这些文章很有帮助,但还有很多不足之处。
https://www.carlrippon.com/httpcontext-in-asp-net-core/ https://dotnetcoretutorials.com/2017/01/05/accessing-httpcontext-asp-net-core/
在控制器内部,它似乎是最简单的,但大部分HttpContext.Current内容都散布在域库等各个地方。
因此,在上面的两个网址中,似乎使用下面的这一行startup.cs
,但是,我在主 asp.net core 之外没有startup.cs
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
Run Code Online (Sandbox Code Playgroud)
然后在启动时我也看到了这篇文章 https://www.strathweb.com/2016/12/accessing-httpcontext-outside-of-framework-components-in-asp-net-core/
//one of many things added to project
services.AddHttpContextAccessor();
Run Code Online (Sandbox Code Playgroud)
我意识到在过去几年中已经提出了几个问题 - 希望一些勇敢的灵魂已经探索过这些东西,并对如何迁移这些东西有一些建议/答案。
移植方法:#1
public Service(string key, LogRecord.CallType callType, string operation, string opArg, string opResponse)
{
this.Key = string.IsNullOrEmpty(key)
? HttpContext.Current != …Run Code Online (Sandbox Code Playgroud) asp.net-core ×6
c# ×6
.net-core ×2
httpcontext ×2
session ×2
.net ×1
asp-classic ×1
asp.net ×1
asp.net-2.0 ×1