6

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?

8
  • 1
    should I maybe make a attribute? why? Did you checked your class base properties in the documentation? Commented Oct 8, 2019 at 13:21
  • hmm, checking the documentation of httpcontext? Commented Oct 8, 2019 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? Commented Oct 8, 2019 at 13:30
  • hmm, checking the documentation of httpcontext? are you extending your class from which contains Random method from HttpContext or rather Controller ? Commented Oct 8, 2019 at 13:30
  • im on asp.net mvc Commented Oct 8, 2019 at 13:31

1 Answer 1

7

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();
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

im on asp.net mvc. im not completely sure what controller class definition is, but im following a course, and everything should be fine.
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?
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.
im really quite confused what is meant by controller definition? is it the routing from url to controller?
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.