草庐IT

httpContext

全部标签

c# - 为什么等待后 HttpContext.Current 为空?

我有以下测试WebAPI代码,我没有在生产中使用WebAPI,但我之所以这样做是因为我对这个问题进行了讨论:WebAPIAsyncquestion无论如何,这是有问题的WebAPI方法:publicasyncTaskGet(intid){varx=HttpContext.Current;if(x==null){//notthrownthrownewArgumentException("HttpContext.Currentisnull");}awaitTask.Run(()=>{Task.Delay(500);id=3;});x=HttpContext.Current;if(x==nu

c# - 在 ASP.NET Core 中访问当前的 HttpContext

我需要在静态方法或实用程序服务中访问当前的HttpContext。对于经典的ASP.NETMVC和System.Web,我只需要使用HttpContext.Current来静态访问上下文。但我如何在ASP.NETCore中执行此操作? 最佳答案 HttpContext.Current在ASP.NETCore中不再存在,但有一个新的IHttpContextAccessor您可以注入(inject)依赖项并用于检索当前的HttpContext:publicclassMyComponent:IMyComponent{privateread

c# - 如何在 ASP.NET Core 中获取 HttpContext.Current?

这个问题在这里已经有了答案:AccessthecurrentHttpContextinASP.NETCore(7个答案)关闭6年前。我们目前正在使用ASP.NETCore重写/转换我们的ASP.NETWebForms应用程序。尽量避免重新设计。有一节我们在类库中使用HttpContext来检查当前状态。如何在.NETCore1.0中访问HttpContext.Current?varcurrent=HttpContext.Current;if(current==null){//dosomethinghere//stringconnection=Configuration.GetConne

c# - 在测试初始化​​方法中模拟 HttpContext.Current

我正在尝试将单元测试添加到我构建的ASP.NETMVC应用程序中。在我的单元测试中,我使用以下代码:[TestMethod]publicvoidIndexAction_Should_Return_View(){varcontroller=newMembershipController();controller.SetFakeControllerContext("TestUser");...}使用以下助手来模拟Controller上下文:publicstaticclassFakeControllerContext{publicstaticHttpContextBaseFakeHttpCo

c# - 在单元测试中设置 HttpContext.Current.Session

我有一个网络服务,我正在尝试对其进行单元测试。在服务中,它从HttpContext中提取几个值,如下所示:m_password=(string)HttpContext.Current.Session["CustomerId"];m_userID=(string)HttpContext.Current.Session["CustomerUrl"];在单元测试中,我使用简单的工作请求创建上下文,如下所示:SimpleWorkerRequestrequest=newSimpleWorkerRequest("","","",null,newStringWriter());HttpContext

c# - ASP.NET Identity - HttpContext 没有 GetOwinContext 的扩展方法

我已从此处下载并成功运行了ASP.NETIdentity示例:https://github.com/rustd/AspnetIdentitySample我现在正在我的项目中实现ASP.NETIdentity框架,遇到了一个问题,这个问题让我发疯了一整天...GetOwinContext()doesnotexistasanextensionmethodonmyHttpContext我正在类库中实现身份框架。我已经安装了所有最新(预发布版本)的身份框架,除此之外一切正常。我已经尝试在我的Controller中直接实现相同的代码,但发现了同样的问题。我显然在某处遗漏了一个引用,虽然我不知道是

关于异步操作后的 c#:HttpContext.Current.Items

HttpContext.Current.ItemsafteranAsyncoperation考虑以下ASP.NETWebAPI委托处理程序:1234567891011121314151617181920publicclassMyHandler:DelegatingHandler{  protectedasyncoverrideTaskHttpResponseMessage>SendAsync(HttpRequestMessagerequest,System.Threading.CancellationTokencancellationToken)  {    varguid=Guid.NewG

关于异步操作后的 c#:HttpContext.Current.Items

HttpContext.Current.ItemsafteranAsyncoperation考虑以下ASP.NETWebAPI委托处理程序:1234567891011121314151617181920publicclassMyHandler:DelegatingHandler{  protectedasyncoverrideTaskHttpResponseMessage>SendAsync(HttpRequestMessagerequest,System.Threading.CancellationTokencancellationToken)  {    varguid=Guid.NewG