Questions tagged [httpcontext]

HttpContext is a class from .NET Framework which provides information about an HTTP request.

HttpContext is a class from .NET Framework which provides information about an HTTP request.

1127 questions
26
votes
3 answers

HttpContext not available in Class Library

I am working on a project where I have a C# class library which needs to use the System.web.HttpContext. I've done this before in another project without problem but now its not working. I'm not sure what I am missing they're both targeting .net 3.5…
Boardy
  • 35,417
  • 104
  • 256
  • 447
24
votes
8 answers

What is the difference between these two HttpContext.Current.Session and Session - asp.net 4.0

What is the difference between these 2 piece of codes. HttpContext.Current.Session["myvariable"] Session["myvariable"] asp.net 4.0 and C# 4.0
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
23
votes
2 answers

System.Web.HttpContext.Current.User.Identity.Name Vs System.Environment.UserName in ASP.NET

What is the difference between System.Web.HttpContext.Current.User.Identity.Name and System.Environment.UserName in the context of a ASP.Net Web Application Project? Here's the code of what I'm trying to do: Database myDB =…
FMFF
  • 1,652
  • 4
  • 32
  • 62
22
votes
6 answers

How do I retrieve the HttpContext properties when it returns null?

I am doing some asynchronous work on a separate thread using: ThreadPool.QueueUserWorkItem() and in this separate thread, I need to call HttpContext.Current so that I can access: HttpContext.Current.Cache HttpContext.Current.Server …
makstaks
  • 2,111
  • 4
  • 24
  • 38
22
votes
4 answers

HttpContext.Response.Body.Position = 0 - "Specified method is not supported" error

I've got some logging middleware I've setup that grabs and logs information utilizing HttpContext. I need to set the position of the HttpResponse.Body to 0 in order to read the whole stream, however, no matter what I try, it throws "Specified method…
expenguin
  • 1,052
  • 2
  • 21
  • 42
22
votes
6 answers

What the heck is a context?

I'm starting to see Contexts everywhere I look. In ASP.NET MVC, there are ControllerContexts, RequestContexts, HttpContexts, FormContexts. In Entity Framework, you have ObjectContexts and DbContexts. Ninject has Ninject.Activation.IContext. What…
21
votes
5 answers

HttpContext.Current is null in my web service

I have a web service (.svc), and I am trying to capture the SOAP request using a piece of code found elsewhere on StackOverflow. The problem is that HttpContext.Current is null, so I can't access Request.InputString. Why is this null, and how can…
Pepito Fernandez
  • 2,352
  • 6
  • 32
  • 47
21
votes
2 answers

How to use Server.MapPath when HTTPContext .Current is Nothing

I have some code that works fine when I need to delete some image files from a directory on my web server: Dim ImageURL As String = dsImages.Tables(0).Rows(iImgRow).Item("ImageURL") Dim physicalName =…
John Adams
  • 4,773
  • 25
  • 91
  • 131
21
votes
1 answer

Why should I inject IHttpContextAccessor as a Singleton

In all examples that i've seen of IHttpContextAccessor injection, it is set as a Singleton. Examples: How to add IHttpContextAccessor in the Startup class in the DI in ASP.NET Core 1.0? Injecting IHttpContextAccessor into ApplicationDbContext…
Arthur Rizzo
  • 1,337
  • 15
  • 30
21
votes
2 answers

ASP.NET HttpContext.Current inside Task.Run

I have a following code example that is used in ASP.NET MVC application. The purpose of this code is to create "fire and forget" request for queuing some long running operation. public JsonResult SomeAction() { HttpContext ctx =…
Alex Dn
  • 5,465
  • 7
  • 41
  • 79
21
votes
3 answers

Need to add custom header to request in unit test

I finally was able to get the HttpContext.Current to be not null by finding some code online. But I still have not be able to add custom headers to the request in my unit test. Here is my test: [TestClass] public class TagControllerTest { …
21
votes
3 answers

HttpContext.Current.Response inside a static method

I have the following static method inside a static class. My question is it safe to use HttpContext.Current.Response inside a static method? I want to be 100% sure that it is thread safe and is only associated with the calling thread.. Does anybody…
Rippo
  • 22,117
  • 14
  • 78
  • 117
20
votes
2 answers

How to mock HttpContext.User

I am working on a Asp.net MVC 5 project and I am trying to setup a mock to return a custom principal within a controller. I have search and tried different approach suggested but none of them works. I have a BaseController which all my controllers…
Edmand Looi
  • 3,331
  • 6
  • 19
  • 21
19
votes
2 answers

HttpContext.Features vs HttpContext.Items In Asp.Net Core

What is the differences between these two Properties? I can use HttpContext.Items instead of HttpContext.Features to share data between middlewares. The only difference I see is that I tell Items for a key and it gives me an object and I have to…
Mohammad Barbast
  • 1,753
  • 3
  • 17
  • 28
19
votes
2 answers

Using MapPath from a worker thread

I have a WCF service method that's running in a worker thread I spin from another method. I need to map a relative service app path ("~/Templates/a.template") to the physical path ("D:\Web\Templates\a.template"), but I can't use…
Andrey
  • 20,487
  • 26
  • 108
  • 176
1 2
3
75 76