4

In asp.net how do i get the current httpcontext for the page that i am on?

i have tried giving the relevant controller the httpcontext as an argument, but this does not work. I have found plenty of people describing how to specify the object , but i simply cannot find any info on how the httpcontext should be added to the controller (like, in a argument or so)

this does not work

    public class MovieController : Controller
    {
            public ActionResult Random(HttpContext mycontext)
        {

            RandomMovieViewmodel rmvm = new RandomMovieViewmodel();


            return View(rmvm);
        }

How do i acces the context? should I maybe make a attribute?

s3j80
  • 143
  • 1
  • 1
  • 9
  • 1
    *should I maybe make a attribute?* why? Did you checked your class base properties in the documentation? – Selvin Oct 08 '19 at 13:21
  • hmm, checking the documentation of httpcontext? – s3j80 Oct 08 '19 at 13:30
  • 3
    ASP.NET covers a lot of stacks - WebForms, MVC, Web API, ASP.NET Core. All of them have a Context in one way or another. None of them passes it as an action parameter. Which stack are you asking about? – Panagiotis Kanavos Oct 08 '19 at 13:30
  • *hmm, checking the documentation of httpcontext?* are you extending your class from which contains `Random` method from `HttpContext` or rather `Controller` ? – Selvin Oct 08 '19 at 13:30
  • im on asp.net mvc – s3j80 Oct 08 '19 at 13:31
  • Selvin: no this is my controller, the random() method is the method that a certain url routes to – s3j80 Oct 08 '19 at 13:34
  • Possible duplicate of [Can't access to HttpContext.Current](https://stackoverflow.com/questions/18309239/cant-access-to-httpcontext-current) – TAHA SULTAN TEMURI Oct 08 '19 at 13:59
  • TAHA SULTAN TEMURI: i did see that one before posting. But as I wrote i my question, this question does not talk about how exactly the httpcontext ends up in the controller, only which kind of object is used – s3j80 Oct 08 '19 at 14:01

1 Answers1

5

Controller definition is really important here.

For example, in .Net Core 2.2 with a Controller derived from ControllerBase, HttpContext exposed as a property.

I'm not sure about your environment or your class definition, but it always similar in Asp.Net MVC. Just make sure that, you defined your Controller class correctly.

UPDATE

When you derived from Controller class, HttpContext exposed as property. You can directly use it.

public class TestController : Controller
{
    public ActionResult Index()
    {
        var context = HttpContext;

        return View();
    }
}
Serdar
  • 747
  • 4
  • 12
  • im on asp.net mvc. im not completely sure what controller class definition is, but im following a course, and everything should be fine. – s3j80 Oct 08 '19 at 13:49
  • But the thing im asking about is whether anyone knows how to acces the httpcontext object for that page? is it a question that makes sense? can i do it with the parameter? – s3j80 Oct 08 '19 at 13:50
  • Can you share more detail of your Controller definition? If you can update your question with sample code that will be more easy to understand the problem. – Serdar Oct 08 '19 at 13:53
  • im really quite confused what is meant by controller definition? is it the routing from url to controller? – s3j80 Oct 08 '19 at 13:56
  • In the question you paste you method (action) definition which should be reside in a Controller class. It should be something like `public class TestController : ControllerBase` – Serdar Oct 08 '19 at 13:57
  • ahh, i see my controller class derives from controller. will update. thank you – s3j80 Oct 08 '19 at 13:58