在mvc3中创建会话 [英] Create Session in mvc3

查看:87
本文介绍了在mvc3中创建会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我是mvc3的新手.

我在accountcontroller中使用此代码创建了会话
静态字符串usernameSessionVar =用户名";

Hi,
I am new in mvc3.

I have Create Session in accountcontroller with this code
static string usernameSessionVar = "username";

System.Web.HttpContext.Current.Session[usernameSessionVar] =id.user_id; System.Web.HttpContext.Current.Session.Timeout = 2880;


并且可以使用此代码在其他控制器中进行访问


and that can be access in other controller with this code

static string usernameSessionVar = "username";
 int id=Convert.ToInt16(System.Web.HttpContext.Current.Session[usernameSessionVar]); 



在本地服务器上工作正常.但是当网站上载时,它不起作用.
请问我如何在mvc3中创建会话.

谢谢.



it is work fine in local server. but when site upload then it is not work.
please me how to create session in mvc3.

Thank You.

推荐答案

会话用于TempData存储. TempData是会话状态的一种非常有限的形式,它将仅持续到特定用户的下一个请求为止. (在MVC 2+中进行编辑,一直持续到下一次读取为止.)TempData的目的是存储数据,然后进行重定向,并使所存储的数据可用于您刚刚重定向到的操作.

将Session用于TempData存储意味着已处理Session的任何分布式缓存系统都将适用于TempData.在TempData可以使用时避免直接使用Session有很多好处.一种是您不必自己清理会话;另一种是您无需自己清理会话. TempData将过期"到
Session is used for the TempData store. TempData is a highly limited form of session state which will last only until the next request from a certain user. (Edit In MVC 2+, it lasts until it is next read.) The purpose of TempData is to store data, then do a redirect, and have the stored data be available to the action to which you just redirected.

Using Session for the TempData store means that any distributed caching system which already handles Session will work for TempData. Avoiding using Session directly when TempData will do has a couple of advantages. One is that you don''t have to clean up the Session yourself; TempData will "expire" on its own.


这篇关于在mvc3中创建会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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