开发者

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

I don't 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 t开发者_如何转开发o create just once. I thought of using ViewData, but all my extension methods that are accessing context information are using HttpContextBase and thought I should the same for this.

Should I?


If you want your object to live for a single request only then you can use HttpContextBase.Items.


Use @ViewContext.Controller.ViewData to access the ViewData of the current controller. Assuming you want to pull this from the View you're currently in. It would help to see a use-case.

ViewData is part of the WebViewPage and not really something that can be access from HttpContextBase itself.

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


You can use a Session to store the information and have it available.

HttpContext.Current.Session
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜