65.9K
CodeProject is changing. Read more.
Home

Cookieless Sessing Using MVC + Post Enabled Forms

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.57/5 (5 votes)

Aug 4, 2011

CPOL

2 min read

viewsIcon

26031

downloadIcon

383

Cookieless sessing using MVC and post enabled forms.

Introduction 

The code provided allows the programmer to use standard functions in order to maintain POST in forms when using a cookieless session and MVC.

Using the code  

Once the classes are added to your project, it's as simple to use the new functions as before. 

<% 
using (Html.BeginFormNoCookies("Claim")) { %>    
     //Form Contents here
<%} %>

Form ID is the only parameter. It is technically not required, except for when Client Validation is enabled.

How it works

The existing BeginForm is just an extension and that is exactly how I implemented this. In fact, I downloaded the source for Microsoft's MVC2 FormExtension. I changed the function names to not cause any confusion. A new parameter had to be added in order to deal with the fact that I wasn't able to maintain the dynamic form ID creation since some existing methods were internal to System.Web.MVC. The next major part was constructing the URL and always including the session ID as well as any actions, IDs, or query parameters.

I created a class called CookielessHelper. This is what creates the URL. The URL is constructed around the Controller. It is always expected to be in the following format: /{Root Information}/{Controller}/{Anything}?{Query}. The session ID is always added before the controller and everything else is always preserved.

Note

This version as of now does not have Route modifications enabled. If you use any other function than the one listed, the session ID will not be added. All other functions are currently commented out until they can be completely implemented. I am sure it is not difficult, but I don't need it at this point.

History

  • 8/4/11 - Linus Concepcion pointed out that I can get the Controller by way of the HTMLHelper. Thanks! (Removed require parameter View Page.)
  • 8/4/11 - Initial release.