页面加载之间的ASP.NET MVC会话丢失 [英] ASP.NET MVC session lost between page loads

查看:168
本文介绍了页面加载之间的ASP.NET MVC会话丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MVC中使用会话状态,并且整个晚上都被卡住了! 我意识到会话状态应该在MVC中谨慎使用-但我确定我想在此页面中使用它-因此,我们会赞赏解决方案,而不是意见.

I am trying to use session state in MVC and I have been stuck for the entire evening! I realise session state should be used sparingly in MVC - but I am sure I want to use it for this one page - so would appreciate solutions rather than opinions.

基本上,我有一个包含验证码图片的联系表格.当页面加载时,我想将会话设置为CAPTCH图像(动态生成的图像)​​中使用的字符. 我有一个新图像"链接,该链接异步创建新代码,设置会话并动态将新图像加载到屏幕上.

Basically, I have a contact form with a CAPTCHA image. When the page loads I want to set the session to the characters used in the CAPTCH image (dynamically generated image). I have an 'new image' link which async creates a new code, sets the session and dynamically loads a new image to screen.

只要页面未加载或重新加载,会话就保持不变.我需要能够根据会话中的代码验证用户输入(该代码应反映向用户显示的内容),但是会话为空.

The session stays sets as long as the page doesn't load or reload. I need to be able to validate the user input against the code in session (which should reflect what is displayed to the user) but the session is empty.

如果我在图像上执行AJAX重新加载,即异步设置会话-在执行发布时设置会话!

If I perform an AJAX reload on the image i.e. set the session asynchronously - the session is set when I perform a post!!

这是怎么回事?

我需要能够保留会话值-arrrhhhh!

I need to be able to persist the session value - arrrhhhh!

我有一个基本控制器:

public new HttpContextBase HttpContext
{
   get
   {
      HttpContextWrapper context = new HttpContextWrapper(System.Web.HttpContext.Current);
      return (HttpContextBase)context;
   }
 } 

在控制器中,我有:

  [AcceptVerbs(HttpVerbs.Post)]
  public ActionResult Contact(ContactForm c, string button)
  {
    string sessCaptcha = HttpContext.Session["CAPTCHA_Contact"] == null ? "" : HttpContext.Session["CAPTCHA_Contact"].ToString();
  }

任何想法???上面放着一颗樱桃很漂亮:)

Any ideas????? pretty pls with a cherry on top :)

谢谢!

推荐答案

我现在很尴尬...

永远不要在Response.End()之前设置会话值(我相信对response.redirect也是如此).

Never ever set a session value before a Response.End() (and i believe also the same applies to response.redirect).

OMG-这是4个小时,我再也不会回来了!

OMG - that's 4 hours I will never ever get back again!

这是演示代码,以演示我的无能...

Here is the demo code to illustrate my ineptitude...

   public ActionResult Page1()
        {
            Session["test1"] = "hello world";

            // This means the session won't be set
            // It's is used in valid circumstances..e.g. setting session, then dynamically generating an image based on that value.
            Response.End();

            return View();
        }

        public ActionResult Page2()
        {
            ViewBag.Test = Session["test1"].ToString();

            return View();
        }

        [HttpPost]
        public ActionResult Page2(FormCollection fc)
        {
            ViewBag.Test = "...is still..." + Session["test1"].ToString();

            return View();
        }

这篇关于页面加载之间的ASP.NET MVC会话丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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