How to access ViewData from HttpContext? (ASP.Net MVC)

I do not know if this is the right approach, but correct the question if necessary.

I need to access an object through the full page life cycle that I want to create only once. I was thinking about using ViewData, but all my extension methods that access contextual information use HttpContextBase and think that I should do the same for this.

Should I?

+6
source share
3 answers

If you want your object to live for only one request, you can use HttpContextBase.Items .

+17
source

Use @ViewContext.Controller.ViewData to access the ViewData current controller. Assuming you want to get this out of the view you are currently in. This will help to see a precedent.

ViewData is part of WebViewPage and is not something accessible from HttpContextBase .

After reading your comment on another answer, you can use HttpContext.Current.Items to create items that are available only for the current request.

+3
source

You can use a session to store information and its availability.

 HttpContext.Current.Session 
-2
source

Source: https://habr.com/ru/post/899254/


All Articles