How to access HttpContext from controller in ASP.NET mvc
Asked Answered
N

1

5

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?

Nonrestrictive answered 8/10, 2019 at 13:18 Comment(8)
should I maybe make a attribute? why? Did you checked your class base properties in the documentation?Eada
hmm, checking the documentation of httpcontext?Nonrestrictive
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?Outdistance
hmm, checking the documentation of httpcontext? are you extending your class from which contains Random method from HttpContext or rather Controller ?Eada
im on asp.net mvcNonrestrictive
Selvin: no this is my controller, the random() method is the method that a certain url routes toNonrestrictive
Possible duplicate of Can't access to HttpContext.CurrentRenatorenaud
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 usedNonrestrictive
A
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();
    }
}
Aluin answered 8/10, 2019 at 13:40 Comment(6)
im on asp.net mvc. im not completely sure what controller class definition is, but im following a course, and everything should be fine.Nonrestrictive
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?Nonrestrictive
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.Aluin
im really quite confused what is meant by controller definition? is it the routing from url to controller?Nonrestrictive
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 : ControllerBaseAluin
ahh, i see my controller class derives from controller. will update. thank youNonrestrictive

© 2022 - 2024 — McMap. All rights reserved.