在 ASP.Net MVC 中的视图页面上访问会话 [英] Access session on view page in ASP.Net MVC

查看:46
本文介绍了在 ASP.Net MVC 中的视图页面上访问会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过在控制器中创建公共方法来访问视图页面上的会话,也想通过 ASP.Net MVC 中的公共方法访问控制器中的会话.

Is there any way to access session on view page by creating common method in controller also want to access session in controller by common method in ASP.Net MVC.

推荐答案

你在使用 razor 视图引擎吗?

are you using razor view engine?

查看:

@{ var sessionVar = Session["key"]; //it's object }

控制器:

public ActionResult Method() {
 var sessionVar = this.Session["..."]; //
}

调用这个对象的常用方法是:HttpContext.Current.Session

the common way to call this object is: HttpContext.Current.Session

我不知道你所说的普通"是什么意思.无论您在哪里调用它,提供的会话对象对于用户会话都是通用的.

I don't know what you meant by 'common'. the provided session object is common for user session no matter where you will call for it.

但实际上,您不应该尝试使用会话,因为它很丑 - 尝试对 ViewBag/ViewData 进行一些搜索,然后尝试搜索您不应该使用它们的原因.:)

But in fact you shouldn't try to use session it's ugly - try to do some search about ViewBag / ViewData and then try to search why you shouldn't use them as well. :)

这篇关于在 ASP.Net MVC 中的视图页面上访问会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆