Im developing sites that are targeting mobile phones and i can therefore not trust that the clients are supporting cookies to enable sessions.
Im therefore forced to use the "UseUri" mode of parameter cookieless in the sessionstate configuration that will include the session id in the urls.
For a number of reasons i want to move this session id to the query string instead of having it as a directory, e.g. "/(S($SESSION_ID$)/Products/Search" to "/Products/Search?id=$SESSION_ID$.
I have already found out how to override asp.nets way of intializing the incoming http requests sessions from the querystring, this is done by giving an custom implementation of interface ISessionIDManager . This imlementation resolves the session id from the
qurerystring without no problem.
The thing I now need to solve is to someway override the MVC framework / ASP.NETs way to print links, again i need to override the defautl behaviour to include the session id in the directory to the querystring. I have read but not confirmed that in ASP.NET
the mehtod that includes the session id in a clean URL is method ApplyAppPathModifier of the HttpResponse object.
Before i started to use MVC i just hade done my own control that would output the session id in the querystring. But how do i do this if i want to use all the various html helper classes generating MVC links, e.g. HtmlHelper.ActionLink.
You can just add the session id into the url for example:
Then you would have a route to pick up the sessionId parameter for example:
routes.MapRoute
(
"RouteName",
"{controller}/{action}/{sessionId},
new {controller="MyController", action="MyAction"}
);
If you're going to add the session id to every link, it would be worth creating a helper method that wraps the html helpers and inserts the sessionId parameter to each url.
Hope this helps.
Kind Regards,
Sean.
Please click "Mark as Answer" on this post if it helps you.
I dont think that solution will help me unfortunatley. The thing is that when ASP.NET is configured with sessionstate as i explained before it will insert the session id in the beginning of the URL.:
Even if I would write a helper method that could automatically append the session id to a querystring i still would have the URL starting with the session id, i need to remove the session id in the start.
I have tried to debug the framework and i can see that injection of the session id is happening in method UrlHelper.GenerateUrl(....n) . This method is calling the GetVirtualPath on the RouteCollection class and gives the requestContext as an argument. The
argumen requestContext has a Response property of type System.Web.HttpResponseWrapper and I guess that it it this objects mehtod "ApplyPathModifier" that finally injects the session id. So the big question is how i can override this method? My guess is that
i need to look how the requestContext is intially initialized and the perhaps wrap this httpcontext with my own implementation. Again, if anyone could help me out here i would be very happy. Cant say that Im at all sure on what im doing here :-)
expression in debugger from the GenerateUrl method. :((System.Web.HttpContextWrapper)requestContext.HttpContext).Response.ApplyAppPathModifier()
Based on my testting, you can do it as Sean suggested. But you have to delete the
cookieless="UseUri"
in web.config. And I think it's not necessary in mvc to use the declaration. You can try it and let me know whether it is helpful. Thanks.
Hi Chen, How should i put it........ right now I feel quite stupid :-) Of course you are right, why on earth should I first enable my problem by setting the cookieless property to useUri resulting with the behaviour that i dont want to have and need to remove......
Idito i am!!!
Instead, i will use the normal cookie settings that will send the session id to the client.
When i print my ActionLinks i will append a querystring with the session id.
And finally my SessionIdManager will look for the session id in the querystring
Finished!
However, i would like to ask a follow up question around ActionLinks. What would be the best way to implement my desired behaviour "globally".
Of course i can write my own extension methods for the Html object that actually are wrapping all the Action links that are coming with the framework. However, im a bit afraid that our developers would then by misstake choose the wrong methods by accident,
choosing one of the actionLink mehtods that comes with the framework and would result in links without the session id appended.
Can you think of any solution how I could prevent this, i need somehow to decorate all ActionLink mehtods automatically, e.g. that i get some kind of callback when an action link has been created so i can then modify the url.
Anyway, many many thanks for your reply even if i now feel like an idiot
I am sorry that I didn't understand the 'desired behaviour globally' about ActionLinks .Do you mean the absolute path? If it is, you can check some overrided methods of Html.ActionLink, such as:
The thing is now when i know how to overcome my idiotic problem about how to handle the session id i need to figure out how i can change the asp.net mvc framework to always output links with my desired format, sessionid should be appended in the querystring,
e.g. /products/detail/1?sessionid. Of course i can write my own extension mehtods that basically wraps all the mvc asp.net mehtods outputing links and in these wrappers do my session id appending. But isnt there a more nicer way to do this so i dont have to
write all these wrapper methods. If i write my own i always risk that our developers would use one of the frameworks methods and thereby breaking my session handling. I mean, isnt there a way that i basically could register some kind of a callback mehtod when
creating new action links that would be called so i could do my session id trick. Again im afraid if i write my own extension mehtods that the developers could accedentially use the wroing methods resulting in links without the session id appended in the querystring.
Perhapas this is impossible but i would like to hear from you / the community if someon has a good idea how i could implement such a decoration smoothly and safe.
I think the ActionLink method above is enough for your requirement. In the method, we can detemine which method is used to process the request by assigning the action name and the controller name. The session can be sent to server in the form of routeValues.
niro
0 Points
10 Posts
Moving SessionID to the querystring?
Oct 12, 2009 07:35 AM|LINK
Hello,
Im developing sites that are targeting mobile phones and i can therefore not trust that the clients are supporting cookies to enable sessions.
Im therefore forced to use the "UseUri" mode of parameter cookieless in the sessionstate configuration that will include the session id in the urls.
For a number of reasons i want to move this session id to the query string instead of having it as a directory, e.g. "/(S($SESSION_ID$)/Products/Search" to "/Products/Search?id=$SESSION_ID$.
I have already found out how to override asp.nets way of intializing the incoming http requests sessions from the querystring, this is done by giving an custom implementation of interface ISessionIDManager . This imlementation resolves the session id from the qurerystring without no problem.
The thing I now need to solve is to someway override the MVC framework / ASP.NETs way to print links, again i need to override the defautl behaviour to include the session id in the directory to the querystring. I have read but not confirmed that in ASP.NET the mehtod that includes the session id in a clean URL is method ApplyAppPathModifier of the HttpResponse object.
Before i started to use MVC i just hade done my own control that would output the session id in the querystring. But how do i do this if i want to use all the various html helper classes generating MVC links, e.g. HtmlHelper.ActionLink.
Any ideas are greatly apprectiated!
Regards Niclas
SeanMcAlinde...
Member
581 Points
59 Posts
Re: Moving SessionID to the querystring?
Oct 12, 2009 12:42 PM|LINK
Hiya,
You can just add the session id into the url for example:
If you're going to add the session id to every link, it would be worth creating a helper method that wraps the html helpers and inserts the sessionId parameter to each url.
Hope this helps.
Kind Regards,
Sean.
Sean McAlinden - Senior Developer
niro
0 Points
10 Posts
Re: Moving SessionID to the querystring?
Oct 12, 2009 05:50 PM|LINK
Hi there and many thanks for your reply!!
I dont think that solution will help me unfortunatley. The thing is that when ASP.NET is configured with sessionstate as i explained before it will insert the session id in the beginning of the URL.:
<sessionState mode="InProc" cookieless="UseUri" regenerateExpiredSessionId="true"/>
and will result in a URL like:
http://localhost:2310/(S(nq4hdrqsacvjhn55shoy5wm1))/default.aspx
Even if I would write a helper method that could automatically append the session id to a querystring i still would have the URL starting with the session id, i need to remove the session id in the start.
I have tried to debug the framework and i can see that injection of the session id is happening in method UrlHelper.GenerateUrl(....n) . This method is calling the GetVirtualPath on the RouteCollection class and gives the requestContext as an argument. The argumen requestContext has a Response property of type System.Web.HttpResponseWrapper and I guess that it it this objects mehtod "ApplyPathModifier" that finally injects the session id. So the big question is how i can override this method? My guess is that i need to look how the requestContext is intially initialized and the perhaps wrap this httpcontext with my own implementation. Again, if anyone could help me out here i would be very happy. Cant say that Im at all sure on what im doing here :-)
expression in debugger from the GenerateUrl method. :((System.Web.HttpContextWrapper)requestContext.HttpContext).Response.ApplyAppPathModifier()
SeanMcAlinde...
Member
581 Points
59 Posts
Re: Moving SessionID to the querystring?
Oct 12, 2009 10:29 PM|LINK
I see it's a bit more complex than I originally thought - I imagine that not too many people have come across this issue - it would probably be worth taking a look at the mvc source to see exactly what's happening. You can download from here - http://www.microsoft.com/downloads/details.aspx?FamilyID=53289097-73ce-43bf-b6a6-35e00103cb4b&displaylang=en
Sorry I can't be much more help - but I'm sure when you look through the source it will give you clearer idea of how to proceed.
I'll ask some of the guys at work tomorrow if they've come across it, if so I'll let you know.
Kind Regards,
Sean.
Sean McAlinden - Senior Developer
niro
0 Points
10 Posts
Re: Moving SessionID to the querystring?
Oct 13, 2009 06:55 AM|LINK
Hey Sean, yeah i think you are right, its at least to me quite complex and im a bit afraid to change something that i dont have full understanding of.
Perhaps anyone at Microsoft could help me out here?
Niclas
MVC State Maintain
KeFang Chen ...
Star
8329 Points
852 Posts
Re: Moving SessionID to the querystring?
Oct 14, 2009 07:14 AM|LINK
Hi niro,
Based on my testting, you can do it as Sean suggested. But you have to delete the cookieless="UseUri" in web.config. And I think it's not necessary in mvc to use the declaration. You can try it and let me know whether it is helpful. Thanks.
haack
niro
0 Points
10 Posts
Re: Moving SessionID to the querystring?
Oct 14, 2009 02:36 PM|LINK
Hi Chen, How should i put it........ right now I feel quite stupid :-) Of course you are right, why on earth should I first enable my problem by setting the cookieless property to useUri resulting with the behaviour that i dont want to have and need to remove...... Idito i am!!!
Instead, i will use the normal cookie settings that will send the session id to the client.
When i print my ActionLinks i will append a querystring with the session id.
And finally my SessionIdManager will look for the session id in the querystring
Finished!
However, i would like to ask a follow up question around ActionLinks. What would be the best way to implement my desired behaviour "globally".
Of course i can write my own extension methods for the Html object that actually are wrapping all the Action links that are coming with the framework. However, im a bit afraid that our developers would then by misstake choose the wrong methods by accident, choosing one of the actionLink mehtods that comes with the framework and would result in links without the session id appended.

Can you think of any solution how I could prevent this, i need somehow to decorate all ActionLink mehtods automatically, e.g. that i get some kind of callback when an action link has been created so i can then modify the url.
Anyway, many many thanks for your reply even if i now feel like an idiot
Nic
KeFang Chen ...
Star
8329 Points
852 Posts
Re: Moving SessionID to the querystring?
Oct 15, 2009 01:54 AM|LINK
Hi niro,
I am sorry that I didn't understand the 'desired behaviour globally' about ActionLinks .Do you mean the absolute path? If it is, you can check some overrided methods of Html.ActionLink, such as:
If I made a mistake, please let me know. Thanks.
niro
0 Points
10 Posts
Re: Moving SessionID to the querystring?
Oct 19, 2009 11:25 AM|LINK
Hey there and again thanks for your reply!
The thing is now when i know how to overcome my idiotic problem about how to handle the session id i need to figure out how i can change the asp.net mvc framework to always output links with my desired format, sessionid should be appended in the querystring, e.g. /products/detail/1?sessionid. Of course i can write my own extension mehtods that basically wraps all the mvc asp.net mehtods outputing links and in these wrappers do my session id appending. But isnt there a more nicer way to do this so i dont have to write all these wrapper methods. If i write my own i always risk that our developers would use one of the frameworks methods and thereby breaking my session handling. I mean, isnt there a way that i basically could register some kind of a callback mehtod when creating new action links that would be called so i could do my session id trick. Again im afraid if i write my own extension mehtods that the developers could accedentially use the wroing methods resulting in links without the session id appended in the querystring. Perhapas this is impossible but i would like to hear from you / the community if someon has a good idea how i could implement such a decoration smoothly and safe.
Niclas
KeFang Chen ...
Star
8329 Points
852 Posts
Re: Moving SessionID to the querystring?
Oct 20, 2009 02:17 AM|LINK
Hi,
I think the ActionLink method above is enough for your requirement. In the method, we can detemine which method is used to process the request by assigning the action name and the controller name. The session can be sent to server in the form of routeValues.