Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4035652
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:07:30+00:00 2026-05-20T12:07:30+00:00

I am trying to use session state in MVC and I have been stuck

  • 0

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.

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.

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

What’s going on?

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

I have this is a base controller:

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

and in the controller I have:

  [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 🙂

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T12:07:31+00:00Added an answer on May 20, 2026 at 12:07 pm

    I am so embarrased right now…

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

    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();
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use PHP session without using cookies. I have enabled session.use_trans_sid
I have a MVC 2.0 site that uses SQL Server as it's session state
I have a WCF service configured to use ASP.NET session state. I have tested
I am trying to use session data on multiple subdomains: www.example.com my.example.com test.example.com whateversub.example.com
I am trying use gem tire to search in my application. I have tables
Hi I'm trying use a datepicker on a field I have. I'm trying to
I'm trying to use the built-in .NET session handling to share sessions across multiple
When I have a specific setting in web.config, say the session state configuration, and
I know there are frameworks that exist, but I am trying to use wsgi
I am trying to pass data between two pages using Session State in ASP.NET.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.