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.

1046 questions
195
votes
14 answers

Setting HttpContext.Current.Session in a unit test

I have a web service I am trying to unit test. In the service it pulls several values from the HttpContext like so: m_password = (string)HttpContext.Current.Session["CustomerId"]; m_userID = (string)HttpContext.Current.Session["CustomerUrl"]; in…
DaveB
  • 2,029
  • 2
  • 13
  • 11
188
votes
4 answers

Mock HttpContext.Current in Test Init Method

I'm trying to add unit testing to an ASP.NET MVC application I have built. In my unit tests I use the following code: [TestMethod] public void IndexAction_Should_Return_View() { var controller = new MembershipController(); …
nfplee
  • 6,691
  • 12
  • 50
  • 113
156
votes
3 answers

How do I get an HttpContext object from HttpContextBase in ASP.NET MVC 1?

I'm working with some WebForms/MVC-agnostic tools, and I need to get an instance of HttpContext given a reference to an HttpContextBase object. I can't use HttpContext.Current because I need this to work asynchronously as well (HttpContext.Current…
Daniel Schaffer
  • 52,912
  • 28
  • 108
  • 161
106
votes
5 answers

How do I mock the HttpContext in ASP.NET MVC using Moq?

[TestMethod] public void Home_Message_Display_Unknown_User_when_coockie_does_not_exist() { var context = new Mock(); var request = new Mock(); context .Setup(c => c.Request) …
Geo
  • 8,329
  • 13
  • 61
  • 90
89
votes
6 answers

How to access the HttpServerUtility.MapPath method in a Thread or Timer?

I use a System.Timers.Timer in my Asp.Net application and I need to use the HttpServerUtility.MapPath method which seems to be only available via HttpContext.Current.Server.MapPath. The problem is that HttpContext.Current is null when the…
Costo
  • 5,590
  • 7
  • 30
  • 35
84
votes
4 answers

Get current System.Web.UI.Page from HttpContext?

This is actually a two part question. First,does the HttpContext.Current correspond to the current System.UI.Page object? And the second question, which is probably related to the first, is why can't I use the following to see if the current page…
tsimon
  • 8,122
  • 2
  • 27
  • 40
69
votes
7 answers

How to get working path of a wcf application?

I want to get the working folder of a WCF application. How can I get it? If I try HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath) I get a null reference exception (the Http.Current object is null). What I meant…
Stefan Filip
  • 1,751
  • 3
  • 15
  • 25
66
votes
3 answers

Using HttpContext.Current in WebApi is dangerous because of async

My question is a bit related to this: WebApi equivalent for HttpContext.Items with Dependency Injection. We want to inject a class using HttpContext.Current in WebApi area using Ninject. My concern is, this could be very dangerous, as in WebApi…
Lukas K
  • 5,198
  • 4
  • 17
  • 27
59
votes
4 answers

accessing HttpContext.Request in a controller's constructor

I'm following this ASP.NET MVC tutorial from Microsoft: My code is slightly different, where I'm trying to access HttpContext.Request.IsAuthenticated in the controller's constructor. namespace SCE.Controllers.Application { public abstract class…
ozsenegal
  • 3,815
  • 12
  • 47
  • 63
56
votes
5 answers

Access HttpContext.Current from different threads

I have a C# ASP.NET application which starts about 25 different threads running some methods in a class called SiteCrawler.cs. In HttpContext.Current.Session I want to save the result of the search made by the user and present it to the user when…
Raydk
  • 579
  • 1
  • 4
  • 9
53
votes
4 answers

Request.UserHostAddress issue with return result "::1"

I am trying to get client ip address using HttpContext.Request.UserHostAddress; but it returns ::1. How to solve this?
yohan.jayarathna
  • 3,263
  • 13
  • 53
  • 73
52
votes
3 answers

Get application path without using httpcontext. (asp.net)

How to do it? I don't want to use this: HttpContext.Current.Server.MapPath Is there a similar function that I can call without requiring a httpcontext? For example if a start a thread doing some stuff i cant use the httpcontext, but i still need to…
Tomasi
  • 2,327
  • 5
  • 27
  • 39
51
votes
3 answers

Why is HttpContext.Current null?

I have a value that I use in all the application; I set this in application_start void Application_Start(object sender, EventArgs e) { Dictionary> Panels = new Dictionary>(); List
ar.gorgin
  • 4,141
  • 8
  • 52
  • 89
42
votes
4 answers

elmah: exceptions without HttpContext?

I spawn a thread on Application_Start and would like to log exceptions. There is no Context/HttpContext/HttpContext.Current, so how might I get it to log? At the moment, it does not catch any exception in my threads and if I write…
user34537
42
votes
11 answers

HttpContext.Current.User.Identity.Name is Empty

I have a Silverlight application (using MVC) and when i'm building in visual studio, using Visual Studio Development center, there's no problem, the HttpContext.Current.User.Identity.Name has a Value But when i'm using the same project with IIS 7.5…
Jordy
  • 888
  • 2
  • 9
  • 20
1
2 3
69 70