---
title: "How to get .NET HttpContext for isolated code block"  
description: "How to get .NET HttpContext for isolated code block"  
author: "Anonymous User"  
published: 2014-08-27  
updated: 2023-07-06  
canonical: https://www.mindstick.com/forum/2164/how-to-get-dot-net-httpcontext-for-isolated-code-block  
category: "vb.net"  
tags: ["vb.net", ".net", "c#", "HttpContext", "isolated code block", ".NET HttpContext for isolated code block", "HTTP requests"]  
reading_time: 2 minutes  

---

# How to get .NET HttpContext for isolated code block

In .NET is it possible to get the [HttpContext](https://www.mindstick.com/forum/2131/why-would-httpcontext-not-contain-a-host-header) of the current [page](https://www.mindstick.com/articles/13031/why-to-make-a-wikipedia-page) from within an [external](https://www.mindstick.com/articles/12810/how-to-connect-tablet-to-external-monitor-or-flat-screen-tv-using-computer-adapters) [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp)?

So, for example in my page test1.aspx codebehind I've got:

[Dim](https://www.mindstick.com/interview/561/what-is-the-difference-between-the-dim-public-and-private-statements) blah As New FeedWriter()

blah.Run()

But inside FeedWriter.vb, can I get the HttpContext of test1.aspx? Or would I have to pass it in to Run()? (I'm unwilling to do the latter because FeedWriter implements an [interface](https://www.mindstick.com/articles/12101/interfaces-in-java-extending-interfaces) which will need to be re-written if it's to take [arguments](https://answers.mindstick.com/qa/92535/what-are-the-different-types-of-arguments))

## Replies

### Reply by Aryan Kumar

Sure, I can help you with that. Here are the steps on how to get .NET HttpContext for isolated code block:

1. Import the `System.Web` namespace.
2. Get the current HttpContext object from the `HttpContext.Current` property.

Here is an example of how to get .NET HttpContext for isolated code block:

VB.Net

```plaintext
Imports System.Web

Sub Main()
    ' Get the current HttpContext object.
    Dim httpContext As HttpContext = HttpContext.Current

    ' Do something with the HttpContext object.
    ' For example, you could get the current user's identity.
    Dim user As IPrincipal = httpContext.User

    Console.WriteLine(user.Identity.Name)
End Sub
```

Here is an explanation of the code:

- The `HttpContext.Current` property returns the current HttpContext object.
- The `IPrincipal` interface represents a user's identity.
- The `Identity.Name` property of the `IPrincipal` interface returns the user's name.

### Reply by Sumit Kesarwani

Hi John,

You can use the Current property of the HttpContext class:

HttpContext.Current;


---

Original Source: https://www.mindstick.com/forum/2164/how-to-get-dot-net-httpcontext-for-isolated-code-block

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
