Why Does HttpContext.Current Need To Used Within A Class, But Not A Method

Feb 12, 2011

For instance if I'm inside the Page_Load method and I want to get query string data I just do this:

public partial class Product_Detail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
[code].....

View 4 Replies


Similar Messages:

HttpContext.Current.Session Is Throwing Exception In Business Class

Sep 8, 2010

In our application we have to access session objects in business class. We are using HttpContext.Current.Session to get the session value. In some cases it returns the value but mostly its throwing a null reference exception(Object reference not set to an instance of an object). We have the following code Try

If HttpContext.Current.Session("Username") IsNot Nothing then
' Statements to be executed
End If
Catch ex As Exception
'Log to db
End Try

Here HttpContext.Current.Session("Username") is mostly throwing an exception "Object reference not set to an instance of an object" While debugging we found that HttpContext.Current itself is nothing.

View 2 Replies

HttpContext.Current.Request.UserHostName Is Empty When Called From A Class

Apr 15, 2010

I have various web pages that need to build up a URL to display or place it in an emitted email message. The code I inherited had this value for the name of the webserver in a Public Const in a Public Class called FixedConstants. For example:

Public Const cdServerName As String = "WEBSERVERNAME"

Trying to improve on this, I wrote this:

Public Class UIFunction
Public Shared myhttpcontext As HttpContext
Public Shared Function cdWebServer() As String
Dim s As New StringBuilder("http://")
Dim h As String
h = String.Empty
Try
h = Current.Request.ServerVariables("REMOTE_HOST").ToString()
Catch ex As Exception
Dim m As String
m = ex.Message.ToString() 'Ignore this should-not-occur thingy
End Try
If h = String.Empty Then
h = "SomeWebServer"
End If
s.Append(h)
s.Append("/")
Return s.ToString()
End Function

I've tried different things while debugging such as HttpContext.Current.Request.UserHostName and I always get an empty string which pumps out my default string "SomeWebServer". I know Request.UserHostName or Request.ServerVariables("REMOTE_HOST") works when invoked from a page but why does this return empty when invoked from a called method of a class file (i.e. UIFunction.vb)?

View 2 Replies

Any Nasty Side Affects If I Lock The HttpContext.Current.Cache.Insert Method?

May 4, 2010

Apart from blocking other threads reading from the cache what other problems should I be thinking about when locking the cache insert method for a public facing website.

The actual data retrieval and insert into the cache should take no more than 1 second, which we can live with. More importantly i don't want multiple thread potentially all hitting the Insert method at the same time.

The sample code looks something like:

public static readonly object _syncRoot = new object();

if (HttpContext.Current.Cache["key"] == null)
{
lock (_syncRoot)
{
HttpContext.Current.Cache.Insert("key", "DATA", null, DateTime.Now.AddMinutes(5), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
}
}

Response.Write(HttpContext.Current.Cache["key"]);

View 1 Replies

Access The Current Thread's Context Using HttpContext.Current?

Mar 11, 2010

I have a static class with serveral static methods. In these methods, I'm trying to access the current thread's context using HttpContext.Current. For example:

var userName = HttpContext.Current.User.Identity.Name;

However, when I do that, I receive a NullReferenceException, the infamous "Object reference not set to an instance of an object."

View 3 Replies

Difference Between ControllerContext.HttpContext.Session And HttpContext.Current.Session

Nov 23, 2010

i'm using an example in which i can fake the session.It's for use in a unittest of a mvc controller.In the test i create a controller and then i do this:

FakeHttpContext httpctx = new FakeHttpContext(null,null,null,null,mSessionItems );
ControllerContext ctx2 = new ControllerContext(httpctx,new RouteData(), target);

here mSessionItems is my session and target is my instance of a controller,and indeed, when i'm in a controller reading this.ControllerContext.HttpContext.Session, i have a session, great!but.... i also read the session outside the controller, and there i use HttpContext.Current.Session, and that is null (or actualy, the HttpContext.Current is null).

View 1 Replies

How To HttpContext.Current Work?

Aug 5, 2010

I'm wondering how HttpContext.Current gets assigned a unique instance for every request considering it's a static object?

View 2 Replies

Application_PreRequestHandlerExecute: Null HttpContext.Current?

Feb 5, 2010

Is it safe to always assume that HttpContext.Current will be non-null in Application_PreRequestHandlerExecute?

EDIT: Assuming ASP.NET webforms (not MVC).

View 2 Replies

HttpContext.Current / Cache Available To All Sessions

May 4, 2010

As per title. I want to be able to save some data in a cache object but this object must be available to all users/sessions and can expire.What is the best method to achieve this in a asp.net web app?

View 2 Replies

HttpContext.Current.Session Is Null

Aug 20, 2010

I am getting HttpContext.Current.Session is null.Will this be a problem if we have application hosted in web farm.

View 1 Replies

How To Use Server.MapPath When HTTPContext .Current Is Nothing

Jan 20, 2011

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 = Server.MapPath(ImageURL)
oUpload.DeleteFileFromServer(physicalName, iAdid, iImgID)

.but I am running into a problem when a maintenance task running in a separate thread at set intervals determines that files like the above need to be deleted:

Dim ImageURL As String = dsImage.Tables(0).Rows(i - 1).Item("ImageURL")
Dim iImgID As Integer = dsImage.Tables(0).Rows(i - 1).Item("ImageId")
Dim physicalName As String = HttpContext.Current.Server.MapPath(ImageURL)
oUpload.DeleteFileFromServer(physicalName, iAdID, iImgID)

In this latter case, HttpContext.Current.Server.MapPath(ImageURL) has a value of Nothing.Is there a way to get the full path for this case?

View 1 Replies

C# - How To Access HttpContext.Current.Application

Jun 7, 2010

i have problem accessing HttpContext.Current.Application From global.asax its seems to be null every time i try to access it. How can i to this?

HttpContext.Current.Application.Lock();
HttpContext.Current.Application["Actions"] = "hello";
HttpContext.Current.Application.UnLock();

View 2 Replies

How To Access Cache When No HttpContext.Current Is Available

Dec 15, 2010

During Application_End() in Global.aspx, HttpContext.Current is null. I still want to be able to access cache - it's in memory, so want to see if I can reference it somehow to save bits to disk.

Question - is there a way to reference cache in memory somehow when HttpContext.Current is null?

Perhaps I could create a global static variable that would store pointer to cache that I could update on HTTP requests (pseudo: "static <pointer X>" = HttpRequest.Current) and retrieve a reference to cache through that pointer in Application_End()?

Is there a better way to access Cache in memory when there is no Http Request is made?

View 3 Replies

HttpContext.Current.User.Identity.Name With And Without Www?

Oct 20, 2010

am writing a web service in vb.net/asp.net 2, that needs user's name. It works fine when the url request is "www.mysite.com" but if any one has logged-in using "mysite.com" without a "www" and goes to the page that calls my web service, The HttpContext.Current.User.Identity.Name gives null. How can I resolve this problem?

View 2 Replies

Where Does Httpcontext.current.server.mappath Start

Jan 15, 2010

Where does httpcontext.current.server.mappath start?

I don't get it... it doesn't return a path.

My webservice is ran when I debug my application :S.

View 4 Replies

C# - Caching Through HttpContext.Current.Cache Or Just A Static?

Mar 28, 2011

Last night I wrote up my first IHttpModule to do some request processing. I'm using a regular expression to inspect the raw url. The IHttpModule will be called on every request, so it seems reasonable to do some sort of caching of the regular expression object to prevent creation of it on every request.

Now my question... what is better: use the HttpContext.Current.Cache to store the instantiated object or to use a private static Regex in my module?I'm looking forward to the reasons why. Just to clarify: the regex will never change and thus always be the same thing.

View 3 Replies

HttpContext.Current.Application During Load Balancing

Oct 13, 2010

I wanted to understand the behavior of HttpContext.Current.Application during the load balancing. I have read that session can be stored in disk hence can be retrieved from different servers. Is this true for HttpContext.Current.Application also. Does the keydata stored in one machine is accessible in other different machine too?

View 1 Replies

How To Reference A Scriptmanager From HttpContext.Current.CurrentHandler

Mar 28, 2010

I reference the current page in a class with

Page page = HttpContext.Current.CurrentHandler as Page;

I know need to reference ScriptManager1. What the syntax ?

View 2 Replies

How To Get Session Data With Out Having HttpContext.Current, By SessionID

May 5, 2010

I am searching to find a way to read and write on session data but with out having the HttpContext.Current.Why I won to do that ?, because I wish to make some action with the user Session after the page have been close and unloaded. For example, a user load and see a page, then I create a thread to make some action and let user go. Inside this thread I like to read the session data, but in this case HttpContext.Current is not exist any more.

So is there a way to read Session Data knowing just the session id.I store my session inside an sql server, and I see them... its there on table ASPStateTempSessions . How can I read them "offline" and manipulate them ?

View 2 Replies

C# - How To Get The Physical Location Of Web Application Without Using HttpContext.Current

Jun 11, 2010

I've found myself having a requirement to configure log4net based on a file relative to the physical location of the running ASP.NET web application. We like to start the logger as early as possible, so Application_Start seems a proper place. In IIS6, this works fine and has been running for ages, but now we moved to IIS7 and this won't work anymore:

string absolutePath = HttpContext.Current.Request.PhysicalApplicationPath;

because the HttpContext.Current is not available in many global.asax (Application, Session) events. This is old news, we all know it raises the now infamous Request is not available in this context error. We don't want to move back to Classic Mode.

Now, the question is simple: without using HttpContext, is it possible to find the physical location of the currently running web application instance?

View 2 Replies

C# - Set HttpContext.Current.User From Thread.CurrentPrincipal?

May 17, 2010

I have a security manager in my application that works for both windows and web, the process is simple, just takes the user and pwd and authenticates them against a database then sets the Thread.CurrentPrincipal with a custom principal. For windows applications this works fine, but I have problems with web applications.

After the process of authentication, when I'm trying to set the Current.User to the custom principal from Thread.CurrentPrincipal this last one contains a GenericPrincipal. Am I doing something wrong? This is my code:

Login.aspx

[code]....

View 5 Replies

Where Is Origin Of HttpContext.Current.Request.Url.Host

Apr 28, 2010

Why does HttpContext.Current.Request.Url.Host return a different URL than the URL used in the Web browser? For example, when entering "www.someurl.com" in the browser, the HttpContext.Current.Request.Url.Host variable is equal to "www.someotherurl.com".

View 1 Replies

Get The Host Domain Name Without Using HttpContext.Current.Request?

Feb 1, 2010

I've got an ASP .Net application running on IIS7. I'm using the current url that the site is running under to set some static properties on a class in my application. To do this, I'm getting the domain name using this (insde the class's static constructor):

var host = HttpContext.Current.Request.Url.Host;

And it works fine on my dev machine (windows XP / Cassini). However, when I deploy to IIS7, I get an exception: "Request is not available in this context".

I'm guessing this is because I'm using this code in the static constructor of an object, which is getting executed in IIS before any requests come in; and Cassini doesn't trigger the static constructor until a request happens. Now, I didn't originally like the idea of pulling the domain name from the Request for this very reason, but it was the only place I found it =)

So, does anyone know of another place that I can get the host domain name? I'm assuming that ASP .Net has got to be aware of it at some level independent of HttpRequests, I just don't know how to access it.

View 3 Replies

MVC :: HttpContext.Current.Session Item Is Null?

Jan 17, 2011

i'm storing in HttpContext.Current.Session current user, SiteUser is single-tone class that presents current active siteuser, and when he logged i'm creating new SiteUser() in controller and in constructor adding him to the session:

[Code]....

then, with every request to the server services i'm check is user available in session:

[Code]....

otherwise i'm generate non-auth-user exception and redirect him to the logon page. but sometimes HttpContext.Current.Session[sessionKey] is null, but HttpContext.Current.Session doesn't null and FormsAuthenticationTicket is available and Expired property is also false. can somebody help me, why HttpContext.Current.Session[sessionKey] can be null?UPDi have downloaded symbol tables of source .NET Framework and set breakpoints at SessionStateItemCollection on changing collection items. and i resolved some mistakes:1) all collection items are null — "culture" is setting up after2) it happens at the session end eventi can't understand how it can be, because at web.config session timeout is set 20

View 20 Replies

System.ArgumentException While Setting HttpContext.Current.User

Sep 9, 2010

I am trying to configure one http module(a vb module) on MOSS site. In this module I have entry like below:

Dim roles As String() = Nothing
Dim webIdentity As New GenericIdentity(OracleUser, "Form")
Dim principal As New GenericPrincipal(webIdentity, roles)
HttpContext.Current.User = principal
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(HttpContext.Current.User.Identity.Name.ToString, False)
Due to this I am getting 500 internal server and exception on browser is: Exception of type 'System.ArgumentException' was thrown.
Parameter name: encodedValue

If I comment "HttpContext.Current.User" related two lines then it redirect to form login.

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved