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
212
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,199
  • 2
  • 14
  • 11
203
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
  • 7,643
  • 12
  • 63
  • 124
160
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
  • 56,753
  • 31
  • 116
  • 165
115
votes
6 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,663
  • 13
  • 63
  • 93
91
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,940
  • 8
  • 33
  • 35
86
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,362
  • 2
  • 30
  • 41
70
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,791
  • 3
  • 15
  • 25
69
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
  • 6,037
  • 4
  • 23
  • 31
68
votes
6 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
  • 4,055
  • 12
  • 49
  • 64
64
votes
5 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,765
  • 12
  • 61
  • 100
57
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
  • 589
  • 1
  • 4
  • 9
54
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,423
  • 13
  • 56
  • 74
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,517
  • 6
  • 31
  • 43
51
votes
8 answers

How does HttpContext.Current.User.Identity.Name know which usernames exist?

This is not necessarily an issue, I am just curious as to how it works. I have a method: public static bool UserIsAuthenticated() { bool isAuthed = false; try { if (HttpContext.Current.User.Identity.Name != null) { …
Srb1313711
  • 2,017
  • 5
  • 24
  • 35
51
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
  • 978
  • 2
  • 10
  • 20
1
2 3
75 76